By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 1,837 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

finding max value of size_t

P: n/a
Consider the following program

#include <limits.h>
#include <stddef.h>

int main(void)
{
size_t size;
size_t bytes = sizeof(size_t);

if (bytes == sizeof(unsigned int))
size = UINT_MAX;
else if (bytes == sizeof(unsigned long))
size = ULONG_MAX;
else if (bytes == sizeof(unsigned long long))
size = ULLONG_MAX;

return 0;
}

Can this code give the maximum value of size_t in all standard C
compliant compilers.

Mar 9 '07 #1
Share this Question
Share on Google+
22 Replies


P: n/a
su**************@yahoo.com, India said:
Consider the following program

#include <limits.h>
#include <stddef.h>

int main(void)
{
size_t size;
size_t bytes = sizeof(size_t);

if (bytes == sizeof(unsigned int))
size = UINT_MAX;
else if (bytes == sizeof(unsigned long))
size = ULONG_MAX;
else if (bytes == sizeof(unsigned long long))
size = ULLONG_MAX;

return 0;
}

Can this code give the maximum value of size_t in all standard C
compliant compilers.
No, because there is no guarantee that size_t has the same size as any
other integer type. But you can do this:

#include <stddef.h>

int main(void)
{
size_t size = (size_t)-1;
return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 9 '07 #2

P: n/a
(size_t) - 1

Please let me know whether this will give the maximum value of size_t

Mar 9 '07 #3

P: n/a
su**************@yahoo.com, India said:
(size_t) - 1

Please let me know whether this will give the maximum value of size_t
Didn't I just do that? Yes, it will.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 9 '07 #4

P: n/a
Yes. It does give the expected value. Thanks.

The following is a beginner's question.

What is (size_t) ? ie what is parenthesized size_t ? how does it
produce a value ?

Please explain
Mar 9 '07 #5

P: n/a
su**************@yahoo.com, India wrote:
Yes. It does give the expected value. Thanks.

The following is a beginner's question.

What is (size_t) ? ie what is parenthesized size_t ?
Your C book should tell you. (If you haven't got one, get
one; you'll make progress faster than by being drip-fed via
netnews. See the FAQ for suggestions.)

It's a type-name in brackets.

In the expression `(size_t) -1`, it's a /cast/, requesting
a conversion of the expression `-1` to the type `size_t`.

[Note that `-1` is an expression, not a literal, consisting
of the unary minus operator applied to the literal `1`.
It happens to be a /constant expression/, meaning that the
compiler can -- and in some circumstances [1], must -- evaluate
it at compile-time.]
how does it produce a value ?
It converts the value -1 to the guaranteed-unsigned type
`size_t` by adding/subtracting one more than the maximum
value of that type: ie taking it modulo maxvalue+1.

[1] eg as a case label.

--
Chris "electric hedgehog" Dollin
Nit-picking is best done among friends.

Mar 9 '07 #6

P: n/a
su**************@yahoo.com, India wrote:
Yes. It does give the expected value. Thanks.

The following is a beginner's question.

What is (size_t) ? ie what is parenthesized size_t ? how does it
produce a value ?

Please explain

It's a typecast. In this case it's converting the value -1 to the type
size_t -- which is unsigned.
Mar 9 '07 #7

P: n/a
shadowman said:
su**************@yahoo.com, India wrote:
>Yes. It does give the expected value. Thanks.

The following is a beginner's question.

What is (size_t) ? ie what is parenthesized size_t ? how does it
produce a value ?

Please explain

It's a typecast.
Well, close. It's a cast. C doesn't have anything called "typecast".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 9 '07 #8

P: n/a
Richard Heathfield wrote:
shadowman said:
>su**************@yahoo.com, India wrote:
>>Yes. It does give the expected value. Thanks.

The following is a beginner's question.

What is (size_t) ? ie what is parenthesized size_t ? how does it
produce a value ?

Please explain
It's a typecast.

Well, close. It's a cast. C doesn't have anything called "typecast".
I must have been thinking about actors who always play similar roles.
Mar 9 '07 #9

P: n/a
su**************@yahoo.com, India wrote:
(size_t) - 1

Please let me know whether this will give the maximum value of size_t
Please quote context.

Yes it will, not only for size_t, but for any unsigned type, (with the
appropriate cast).

You can also use the SIZE_MAX macro defined in stdint.h, to get the
maximum value of the system's size_t type. It's however a C99 feature.

Mar 9 '07 #10

P: n/a
On Mar 9, 8:14 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Yes. It does give the expected value. Thanks.

The following is a beginner's question.

What is (size_t) ? ie what is parenthesized size_t ? how does it
produce a value ?

Please explain
Assume the statement

size_t size = (size_t) -1;

Starting from the right, the expression '1' is a constant integer
expression, its type is int, and its value is 1. We then apply the
unary negation operator '-' to that expression to get the integer type
value -1.

The expression '(size_t)' is a cast; it's purpose is to convert the
type of the signed integer value -1 to size_t, which is an unsigned
type. When a value falls outside of an unsigned type's range, it
"wraps" to a value within the range that's congruent to the original
value modulo 2^n, where n is the number of bits in the type.

For example, assume an unsigned type with 2 bits; it can store the
values 0, 1, 2, and 3. If we try to store a value that falls outside
of that range, the result will be mapped to the range [0,3]:

Actual value Stored value
------------ ------------
0 0
1 1
2 2
3 3
4 0
5 1
6 2
7 3
8 0
-1 3
-2 2
-3 1
-4 0
-5 3
-6 2
-7 1
-8 0

So, for our imaginary 2-bit unsigned type, the signed integer value -1
maps to the unsigned value 3.

A similar thing happens for converting the signed integer value -1 to
size_t; the resulting value will be the largest value that can be
represented in size_t.

Mar 9 '07 #11

P: n/a
Richard Heathfield wrote:
su**************@yahoo.com, India said:
>Consider the following program

#include <limits.h>
#include <stddef.h>

int main(void)
{
size_t size;
size_t bytes = sizeof(size_t);

if (bytes == sizeof(unsigned int))
size = UINT_MAX;
else if (bytes == sizeof(unsigned long))
size = ULONG_MAX;
else if (bytes == sizeof(unsigned long long))
size = ULLONG_MAX;

return 0;
}

Can this code give the maximum value of size_t in all standard C
compliant compilers.

No, because there is no guarantee that size_t has the same size as any
other integer type. But you can do this:

#include <stddef.h>

int main(void)
{
size_t size = (size_t)-1;
return 0;
}
This appears to work on a 2's compliment machine.

Does it also work on a machine uses sign-magnitude numeric representations?

--
Bill C.
Mar 9 '07 #12

P: n/a
"spacecriter \(Bill C\)" <wc********@sysmatrix.net.yercrankwrites:
Richard Heathfield wrote:
[equivalent to SIZE_MAX]
> size_t size = (size_t)-1;

This appears to work on a 2's compliment machine.

Does it also work on a machine uses sign-magnitude numeric representations?
Yes. The rule for conversion of negative integer value to
a unsigned integer type is to add <type>_MAX+1. It doesn't
depend on the numeric representation.
--
"IMO, Perl is an excellent language to break your teeth on"
--Micah Cowan
Mar 9 '07 #13

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
su**************@yahoo.com, India said:
>Consider the following program

#include <limits.h>
#include <stddef.h>

int main(void)
{
size_t size;
size_t bytes = sizeof(size_t);

if (bytes == sizeof(unsigned int))
size = UINT_MAX;
else if (bytes == sizeof(unsigned long))
size = ULONG_MAX;
else if (bytes == sizeof(unsigned long long))
size = ULLONG_MAX;

return 0;
}
The code in the original article was indented; the indentation was
lost in Richard's followup, at least as I saw it. Probably something
somewhere doesn't like tab characters. Try to use only spaces for
indentation when posting to Usenet. (I've maually re-indented the
code here.)
>Can this code give the maximum value of size_t in all standard C
compliant compilers.

No, because there is no guarantee that size_t has the same size as any
other integer type. But you can do this:

#include <stddef.h>

int main(void)
{
size_t size = (size_t)-1;
return 0;
}
You're right, of coures, that (size_t)-1 is a better solution than the
if/else chain above. A quibble, however: size_t *is* guaranteed to
have the same size as some other integer type. It's a typedef, and it
has to be an alias for something. In C99, however, it could be an
alias for an extended integer type rather than for one of the standard
predefined integer types. (I suppose a C90 implementation could
provide such types as an extension.)

In C90, the type "unsigned long long" doesn't exist (though it's a
common extension). You can either drop "unsigned long long" and have
the code break on implementations where unsigned long long is bigger
than unsigned long and size_t is an alias for unsigned long long, *or*
you can include it and have the code break on systems that don't
support unsigned long long. The __STDC_VERSION__ macro should tell
you what version of C is supported, but there are plenty of
implementations that support unsigned long long without supporting all
of C99.

Implementations are encouraged *not* to make size_t bigger than
unsigned long long unless it's actually necessary. (This is in n1124,
but not in the original C99 standard.) But this doesn't do you much
good.

It's conceivable, though vanishingly unlikely, that size_t is an alias
for unsigned short, or even unsigned char. Perhaps the DS9K does
this.

It's also possible for unsigned int and unsigned long to have the same
size, but for unsigned long to have a wider range (if unsigned int has
padding bits). If size_t on such a system is an alias for unsigned
long, then the above code will quietly produce an incorrect result.

In C99, <stdint.hdefines the SIZE_MAX macro, which is exactly what
you're looking for, but not all implementations provide it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 9 '07 #14

P: n/a
Keith Thompson said:

<snip>
You're right, of coures, that (size_t)-1 is a better solution than the
if/else chain above. A quibble, however: size_t *is* guaranteed to
have the same size as some other integer type.
Sorry, you're right - I should have said that it is not guaranteed to
have the same size as any other *standard* integer type. For example:

typedef _size_type size_t;

is one legal way for an implementation to "declare" size_t, where
_size_type might be a 24-bit type in, say, a C8SI16LP32 system.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 9 '07 #15

P: n/a
Keith Thompson wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:
>su**************@yahoo.com, India said:
[snip]
>>Can this code give the maximum value of size_t in all standard C
compliant compilers.
No, because there is no guarantee that size_t has the same size as any
other integer type. But you can do this:

#include <stddef.h>

int main(void)
{
size_t size = (size_t)-1;
return 0;
}
[snip]
Implementations are encouraged *not* to make size_t bigger than
unsigned long long unless it's actually necessary. (This is in n1124,
but not in the original C99 standard.) But this doesn't do you much
good.
Isn't that even just unsigned long?

================
The types used for size_t and ptrdiff_t should not have an integer
conversion rank greater than that of signed long int unless the
implementation supports objects large enough to make this necessary.
================

Does the above imply that size_t should be a type with sizeof() <
sizeof(long)? ("integer conversion rank greater than that of signed
long int" doesn't sound quite clear)

It indeed doesn't do much good since MS ignores C99 and has long
smaller than size_t in its fancy 64-bit implementation.

Thanks,
Yevgen
Mar 9 '07 #16

P: n/a
Ben Pfaff wrote:
"spacecriter \(Bill C\)" <wc********@sysmatrix.net.yercrankwrites:
>Richard Heathfield wrote:
[equivalent to SIZE_MAX]
>> size_t size = (size_t)-1;

This appears to work on a 2's compliment machine.

Does it also work on a machine uses sign-magnitude numeric
representations?

Yes. The rule for conversion of negative integer value to
a unsigned integer type is to add <type>_MAX+1. It doesn't
depend on the numeric representation.
Cool.

Thanks!

--
Bill C.
Mar 9 '07 #17

P: n/a
Yevgen Muntyan <mu****************@tamu.eduwrites:
Keith Thompson wrote:
>Richard Heathfield <rj*@see.sig.invalidwrites:
>>su**************@yahoo.com, India said:
[snip]
>>>Can this code give the maximum value of size_t in all standard C
compliant compilers.
No, because there is no guarantee that size_t has the same size as
any other integer type. But you can do this:

#include <stddef.h>

int main(void)
{
size_t size = (size_t)-1;
return 0;
}
[snip]
>Implementations are encouraged *not* to make size_t bigger than
unsigned long long unless it's actually necessary. (This is in n1124,
but not in the original C99 standard.) But this doesn't do you much
good.

Isn't that even just unsigned long?
Oops, yes, you're right. (I knew what I *meant* to write!)

[...]
Does the above imply that size_t should be a type with sizeof() <
sizeof(long)? ("integer conversion rank greater than that of signed
long int" doesn't sound quite clear)
Normally, for an implementation that follows the recommendation,
sizeof(size_t) <= sizeof(long). But integer conversion rank isn't
defined in terms of size. It's defined in terms of relationships
between specific types, and the standard elsewhere mandates certain
relationships between the *ranges*, not the sizes, of those types.
(The distinction only matters if some types have padding bits; on most
real-world implementations, they don't.)

Integer conversion rank is defined in C99 6.3.1.1. Briefly (and
incompletely):

Distinct types have distinct ranks even if they have the same
width (e.g., if int and long are both 32 bits, long still has
a greater rank than int).

Corresponding signed and unsigned types have the same rank.

long long long int short signed char

There are additional rules covering _Bool, enumerated types, and
extended integer types, and requiring consistency.
It indeed doesn't do much good since MS ignores C99 and has long
smaller than size_t in its fancy 64-bit implementation.
Yes, even a conforming C99 implementation is allowed to do that.
Apparently MS decided that keeping long at 32 bits was more important
than other considerations. I'd like to see a type system that didn't
require such ugly tradeoffs, but a language with such a system
couldn't reasonably be called C.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 10 '07 #18

P: n/a
On Mar 9, 8:32 pm, "santosh" <santosh....@gmail.comwrote:
subramanian10...@yahoo.com, India wrote:
(size_t) - 1
Please let me know whether this will give the maximum value of size_t
------------------------
Please quote context.
------------------------
>
Yes it will, not only for size_t, but for any unsigned type, (with the
appropriate cast).

You can also use the SIZE_MAX macro defined in stdint.h, to get the
maximum value of the system's size_t type. It's however a C99 feature.
You have mentioned "Please quote context". WHat is meant by quoting
context? I do not understand this. Kindly explain.
Now I have a question on SIZE_MAX.

In the ISO C99 statndard document

http://www.open-std.org.jtc1.sc22.wg...docs.n1124.pdf

in 7.18.3 in page 259
SIZE_MAX is mentioned as 65535

which is (2 raised to the power 16) - 1. Am I right ? But in Redhat
Enterprise Linux gcc implementation, in <stdint.h>, SIZE_MAX value is
defined as 4294967295 which is (2 raised to the power 32) - 1, I
think. I cannot understand this difference. Where am I going wrong.
Please explain

Mar 10 '07 #19

P: n/a
"su**************@yahoo.com, India" <su**************@yahoo.comwrites:
[...]
You have mentioned "Please quote context". WHat is meant by quoting
context? I do not understand this. Kindly explain.
Read <http://cfaj.freeshell.org/google/>.

At one time, Google Groups had a bug that made it very easy to post
followups with no quoted text. That web page started as a complaint
about that bug, along with some suggestions for working around it.
The bug has since been fixed, but the page still has some good
information on the topic, along with some good links.

To summarize: Each followup should quote enough of the parent message
so that the followup makes sense on its own to someone who hasn't seen
the parent.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 10 '07 #20

P: n/a
su**************@yahoo.com, India wrote:
On Mar 9, 8:32 pm, "santosh" <santosh....@gmail.comwrote:
>subramanian10...@yahoo.com, India wrote:
>>(size_t) - 1
Please let me know whether this will give the maximum value of size_t
------------------------
>Please quote context.
------------------------
>Yes it will, not only for size_t, but for any unsigned type, (with the
appropriate cast).

You can also use the SIZE_MAX macro defined in stdint.h, to get the
maximum value of the system's size_t type. It's however a C99 feature.

You have mentioned "Please quote context". WHat is meant by quoting
context? I do not understand this. Kindly explain.
http://www.netmeister.org/news/learn2quote.html
http://cfaj.freeshell.org/google/

http://www.caliburn.nl/topposting.html - but this one doesn't
seem to work right now.
>

Now I have a question on SIZE_MAX.

In the ISO C99 statndard document

http://www.open-std.org.jtc1.sc22.wg...docs.n1124.pdf

in 7.18.3 in page 259
SIZE_MAX is mentioned as 65535

which is (2 raised to the power 16) - 1. Am I right ? But in Redhat
Enterprise Linux gcc implementation, in <stdint.h>, SIZE_MAX value is
defined as 4294967295 which is (2 raised to the power 32) - 1, I
think. I cannot understand this difference. Where am I going wrong.
Please explain
It actually says: 7.18.13-2 "... Its implementation defined value
shall be equal to or greater in magnitude (absolute value) than
the corresponding value given below, with the same sign..." then
it says: SIZE_MAX 65535, which means SIZE_MAX should be at the
very least equal to 65535.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Mar 10 '07 #21

P: n/a
su**************@yahoo.com, India <su**************@yahoo.comwrote:
>You have mentioned "Please quote context". WHat is meant by quoting
context? I do not understand this. Kindly explain.
There's actually a ton of information at the clc wiki about posting
here--everything you want to know and more:

http://clc-wiki.net/wiki/C_community...to_comp.lang.c

(aka http://tinyurl.com/yt9fmc)

-Beej

Mar 10 '07 #22

P: n/a
su**************@yahoo.com, India wrote:
On Mar 9, 8:32 pm, "santosh" <santosh....@gmail.comwrote:
subramanian10...@yahoo.com, India wrote:
(size_t) - 1
Please let me know whether this will give the maximum value of size_t
------------------------
Please quote context.
------------------------

Yes it will, not only for size_t, but for any unsigned type, (with the
appropriate cast).

You can also use the SIZE_MAX macro defined in stdint.h, to get the
maximum value of the system's size_t type. It's however a C99 feature.

Now I have a question on SIZE_MAX.

In the ISO C99 statndard document

in 7.18.3 in page 259
SIZE_MAX is mentioned as 65535

which is (2 raised to the power 16) - 1. Am I right ? But in Redhat
Enterprise Linux gcc implementation, in <stdint.h>, SIZE_MAX value is
defined as 4294967295 which is (2 raised to the power 32) - 1, I
think. I cannot understand this difference. Where am I going wrong.
Please explain
It's value is implementation defined. Different implementations will
each have a suitable value, based on their hardware architecture and
operating system limits, for SIZE_MAX. The Standard specifies the
minimum necessary magnitude.

Mar 10 '07 #23

This discussion thread is closed

Replies have been disabled for this discussion.