472,782 Members | 1,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

finding max value of size_t

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
22 26621
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
(size_t) - 1

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

Mar 9 '07 #3
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
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
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
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
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
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
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
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
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
"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
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
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
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
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
5
by: Robert Manea | last post by:
Hello everyone, I wrote, simply as an exercise, a small piece of code to find 'strings' (defined as an amount of at least 3 ASCII characters followed by a non ASCII character) in binary files. ...
21
by: newlang | last post by:
Hello everyone, I am eager to know about string functions, (user defined) . tell me the technique of find a string in another string.
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
19
by: ramu | last post by:
Hi, I have, suppose 1000 numbers, in a file. I have to find out 5 largest numbers among them without sorting. Can you please give me an efficient idea to do this? My idea is to put those numbers...
22
by: jobo | last post by:
Hello, I'm very new to C so please forgive my ineptitude. If I am given a file with "jfewuuj3uefi8jkw128jdmnsdf\s;'d1904" I want to capture each occurence of an integer 0-9 into an array. ...
7
by: dana_livni2000 | last post by:
how do i print the acuale byte sequence that represent a vairable (let say a char - i want to see the 8 bits). thanks dana
7
by: DarthBob88 | last post by:
I have to go through a file and replace any occurrences of a given string with the desired string, like replacing "bug" with "feature". This is made more complicated by the fact that I have to do...
173
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc....
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.