473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

size_t range

Is there a standard way to determine the max. value of size_t as a
compile-time constant? Will:

#define SIZE_T_MAX ((size_t) -1)

work in all cases, or just on 2s comp. machines?
DrX
Nov 14 '05 #1
16 2214
Xenos wrote:
Is there a standard way to determine the max. value of size_t as a
compile-time constant? Will:

#define SIZE_T_MAX ((size_t) -1)

work in all cases, or just on 2s comp. machines?


It will work in all cases. Casts work on values, not representations.
For what it's worth, C99 defines a SIZE_MAX macro whose value is the
maximum value of size_t.

Jeremy.
Nov 14 '05 #2
"Xenos" <do**********@spamhate.com> wrote in message
news:c5*********@cui1.lmms.lmco.com...
Is there a standard way to determine the max. value of size_t as a
compile-time constant? Will:

#define SIZE_T_MAX ((size_t) -1)

work in all cases, or just on 2s comp. machines?


That's how I understand it. -1, converted to an unsigned type, gives
the maximum value for that type regardless of representation.

Peter
Nov 14 '05 #3
On Fri, 9 Apr 2004 11:17:50 -0400, "Xenos" <do**********@spamhate.com>
wrote:
Is there a standard way to determine the max. value of size_t as a
compile-time constant? Will:

#define SIZE_T_MAX ((size_t) -1)

work in all cases, or just on 2s comp. machines?
DrX

size_t is a typdef for some unsigned integer type. While ((size_t)-1)
will produce the max value that can be stored in that type, there is
no guarantee that any function or operator returning a size_t will
ever produce a value that large. The system is free to limit the
value of a size_t to a lesser value as long as it satisfies the
definition in the standard.

So the return question is do you mean the max value that will fit in a
size_t variable or the largest size of any object per the definition?
<<Remove the del for email>>
Nov 14 '05 #4
In <c5**********@216.39.134.6> Barry Schwarz <sc******@deloz.net> writes:
On Fri, 9 Apr 2004 11:17:50 -0400, "Xenos" <do**********@spamhate.com>
wrote:
Is there a standard way to determine the max. value of size_t as a
compile-time constant? Will:

#define SIZE_T_MAX ((size_t) -1)

work in all cases, or just on 2s comp. machines?
DrX

size_t is a typdef for some unsigned integer type. While ((size_t)-1)
will produce the max value that can be stored in that type, there is
no guarantee that any function or operator returning a size_t will
ever produce a value that large. The system is free to limit the
value of a size_t to a lesser value as long as it satisfies the
definition in the standard.

So the return question is do you mean the max value that will fit in a
size_t variable or the largest size of any object per the definition?


The largest size of any object doesn't make much sense, except on
single user, single tasking platforms, as it can change during the
program's execution, due to other activity on the same system. And the
systems where it makes sense, usually provide it, via an extension.

What really matters is not to exceed (size_t)-1 in an argument to a
function expecting a size_t parameter, because this leads to programming
errors. Consider malloc(100000) on a system with (size_t)-1 == 65535.
The call may succeed, but it won't allocate the expected amount of memory.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
Da*****@cern.ch (Dan Pop) wrote in message news:<c5***********@sunnews.cern.ch>...
In <c5**********@216.39.134.6> Barry Schwarz <sc******@deloz.net> writes:
On Fri, 9 Apr 2004 11:17:50 -0400, "Xenos" <do**********@spamhate.com>
wrote:
Is there a standard way to determine the max. value of size_t as a
compile-time constant? Will:

#define SIZE_T_MAX ((size_t) -1)

work in all cases, or just on 2s comp. machines?
DrX

size_t is a typdef for some unsigned integer type. While ((size_t)-1)
will produce the max value that can be stored in that type, there is
no guarantee that any function or operator returning a size_t will
ever produce a value that large. The system is free to limit the
value of a size_t to a lesser value as long as it satisfies the
definition in the standard.

So the return question is do you mean the max value that will fit in a
size_t variable or the largest size of any object per the definition?


The largest size of any object doesn't make much sense, except on
single user, single tasking platforms, as it can change during the
program's execution, due to other activity on the same system. And the
systems where it makes sense, usually provide it, via an extension.

What really matters is not to exceed (size_t)-1 in an argument to a
function expecting a size_t parameter, because this leads to programming
errors. Consider malloc(100000) on a system with (size_t)-1 == 65535.
The call may succeed, but it won't allocate the expected amount of memory.

Dan

I tried this macro on gcc and on Microsoft Visual Studio C compiler,
the result of (size_t) -1 is still -1. The size_t is typedef as
unsigned int.

However, result of (unsigned char)-1, (unsigned short)-1 are both
correct.

Only (unsigned int) -1 still returns -1. Why is that?

Thanks.
Nov 14 '05 #6
In article <f1*************************@posting.google.com> ,
le********@yahoo.com (Leon) wrote:
I tried this macro on gcc and on Microsoft Visual Studio C compiler,
the result of (size_t) -1 is still -1. The size_t is typedef as
unsigned int.

However, result of (unsigned char)-1, (unsigned short)-1 are both
correct.

Only (unsigned int) -1 still returns -1. Why is that?


How exactly did you figure out that "the result of (size_t) -1 is still
-1" ? I assure you that it is not; for starters, the types of both
expressions are different; one is of type size_t, the other is of type
int.
Nov 14 '05 #7
Christian Bau wrote:

In article <f1*************************@posting.google.com> ,
le********@yahoo.com (Leon) wrote:
I tried this macro on gcc and on Microsoft Visual Studio C compiler,
the result of (size_t) -1 is still -1. The size_t is typedef as
unsigned int.

However, result of (unsigned char)-1, (unsigned short)-1 are both
correct.

Only (unsigned int) -1 still returns -1. Why is that?
How exactly did you figure out that
"the result of (size_t) -1 is still -1" ?


According to my crystal ball, he used the expression,
((size_t)-1 == -1)
I assure you that it is not; for starters, the types of both
expressions are different; one is of type size_t, the other is of type
int.


.... and he didn't realise that the -1 int value,
was getting converted to UINT_MAX.

--
pete
Nov 14 '05 #8
Leon wrote:
.... snip ...
I tried this macro on gcc and on Microsoft Visual Studio C
compiler, the result of (size_t) -1 is still -1. The size_t is
typedef as unsigned int.

However, result of (unsigned char)-1, (unsigned short)-1 are
both correct.

Only (unsigned int) -1 still returns -1. Why is that?


How did you show that result?

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #9
pete <pf*****@mindspring.com> wrote in message news:<40***********@mindspring.com>...
Christian Bau wrote:

In article <f1*************************@posting.google.com> ,
le********@yahoo.com (Leon) wrote:
I tried this macro on gcc and on Microsoft Visual Studio C compiler,
the result of (size_t) -1 is still -1. The size_t is typedef as
unsigned int.

However, result of (unsigned char)-1, (unsigned short)-1 are both
correct.

Only (unsigned int) -1 still returns -1. Why is that?


How exactly did you figure out that
"the result of (size_t) -1 is still -1" ?


According to my crystal ball, he used the expression,
((size_t)-1 == -1)
I assure you that it is not; for starters, the types of both
expressions are different; one is of type size_t, the other is of type
int.


... and he didn't realise that the -1 int value,
was getting converted to UINT_MAX.


Here is what I did,

#include <stdio.h>
#define SIZE_T_MAX ((size_t) -1)
#define UCHAR_MAX ((unsigned char) -1)
#define USHORT_MAX ((unsigned short) -1)
#define UINT_MAX ((unsigned int) -1)

int main()
{

printf("SIZE_T_MAX %d\n", SIZE_T_MAX);
printf("UCHAR_MAX %d\n", UCHAR_MAX);
printf("USHORT_MAX %d\n", USHORT_MAX);
printf("UINT_MAX %d\n", UINT_MAX);
}

and thet result is :

SIZE_T_MAX -1
UCHAR_MAX 255
USHORT_MAX 65535
UINT_MAX -1

I know that (char)-1 is represented as 0xFF as two's complement. so it
doesn't surprise me that (unsigned char)-1 is 255. However, shouldn't
(int)-1 be represented as 0xFFFFFFFF in and thus have a unsigned value
of 2^32 - 1? Basically,I don;t understand why SIZE_T_MAX and UINT_MAX
evaluates to -1.

can you explain the result of my code? Thanks a lot!
Nov 14 '05 #10
Leon wrote:
I tried this macro on gcc and on Microsoft Visual Studio C compiler,
the result of (size_t) -1 is still -1. The size_t is typedef as
unsigned int.


How do you know this? gcc, for example, uses unsigned long for size_t.
And unsigned variables are never -1.
Nov 14 '05 #11
Leon wrote:
Here is what I did,

#include <stdio.h>
#define SIZE_T_MAX ((size_t) -1)
#define UCHAR_MAX ((unsigned char) -1)
#define USHORT_MAX ((unsigned short) -1)
#define UINT_MAX ((unsigned int) -1)

int main()
{

printf("SIZE_T_MAX %d\n", SIZE_T_MAX);
printf("UCHAR_MAX %d\n", UCHAR_MAX);
printf("USHORT_MAX %d\n", USHORT_MAX);
printf("UINT_MAX %d\n", UINT_MAX);


The specifier for an unsigned value is "%u", not "%d" (and for a long
unsigned is "%lu"). You told printf to interpret the argument as
signed, and it did. Do you also use "%d" to print floating values and
then complain that they're not right?
Nov 14 '05 #12
In article <f1*************************@posting.google.com> ,
le********@yahoo.com (Leon) wrote:
Here is what I did,

#include <stdio.h>
#define SIZE_T_MAX ((size_t) -1)
#define UCHAR_MAX ((unsigned char) -1)
#define USHORT_MAX ((unsigned short) -1)
#define UINT_MAX ((unsigned int) -1)

int main()
{

printf("SIZE_T_MAX %d\n", SIZE_T_MAX);
printf("UCHAR_MAX %d\n", UCHAR_MAX);
printf("USHORT_MAX %d\n", USHORT_MAX);
printf("UINT_MAX %d\n", UINT_MAX);
}


%d is used to print values of type int. %d will print the correct values
in the following cases:

1. If the argument is of type int.
2. If the argument is of type char or short, because it will be
promoted to type int.
3. If the argument is of type unsigned char, unsigned short, or
unsigned int, and it is small enough to fit into an int.

On most implementations, unsigned char has less bits than int, so case
(3) applies and (unsigned char) -1 is printed correctly. Use %u instead.

On many implementations, unsigned short has fewer bits than int, so case
(3) applies and (unsigned short) -1 is printed correctly. Won't work on
a 16 bit DOS compiler. Use %u instead.

(unsigned int) -1 is practically guaranteed not to give the correct
result. Use %u instead.

(size_t) -1 is even more guaranteed no to work. size_t can have more
bits than int, and then you are in real trouble. Using %d to print a
value of type size_t is a serious bug in your program.
Nov 14 '05 #13
le********@yahoo.com (Leon) wrote:
pete <pf*****@mindspring.com> wrote:
Christian Bau wrote: <snip>
> How exactly did you figure out that
> "the result of (size_t) -1 is still -1" ?
According to my crystal ball, he used the expression,
((size_t)-1 == -1)
> I assure you that it is not; for starters, the types of both
> expressions are different; one is of type size_t, the other is of type
> int.


... and he didn't realise that the -1 int value,
was getting converted to UINT_MAX.


Here is what I did,

#include <stdio.h>
#define SIZE_T_MAX ((size_t) -1)

<snip>int main()
{
printf("SIZE_T_MAX %d\n", SIZE_T_MAX); <snip>can you explain the result of my code? Thanks a lot!


You can do that yourself, easily. Hint: which kind
of conversion is performed by the %d specifier?

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #14
le********@yahoo.com (Leon) writes:
[...]
Here is what I did,

#include <stdio.h>
#define SIZE_T_MAX ((size_t) -1)
#define UCHAR_MAX ((unsigned char) -1)
#define USHORT_MAX ((unsigned short) -1)
#define UINT_MAX ((unsigned int) -1)

int main()
{

printf("SIZE_T_MAX %d\n", SIZE_T_MAX);
printf("UCHAR_MAX %d\n", UCHAR_MAX);
printf("USHORT_MAX %d\n", USHORT_MAX);
printf("UINT_MAX %d\n", UINT_MAX);
}


The "%d" format expects a signed int argument. In the first call
you're passing an unsigned value and telling printf that it's a signed
value. If you lie to printf, it will probably lie to you.

The printf function has know way of knowing the types of the arguments
you pass it; the only information it has is is the format string.
Getting the types right is entirely the caller's responsibility.

Try this instead:

#include <stdio.h>
#define MY_SIZE_T_MAX ((size_t) -1)
#define MY_UCHAR_MAX ((unsigned char) -1)
#define MY_USHORT_MAX ((unsigned short) -1)
#define MY_UINT_MAX ((unsigned int) -1)

int main()
{
printf("MY_SIZE_T_MAX %lu\n", (unsigned long)MY_SIZE_T_MAX);
printf("MY_UCHAR_MAX %u\n", (unsigned)MY_UCHAR_MAX);
printf("MY_USHORT_MAX %u\n", (unsigned)MY_USHORT_MAX);
printf("MY_UINT_MAX %u\n", MY_UINT_MAX);
return 0;
}

I prefixed all the macros with "MY_", since some of them conflict with
names in <limits.h> (which you didn't #include, but it's best to avoid
re-declaring identifiers declared in standard headers).

I cast MY_SIZE_T_MAX to unsigned long (since it may be bigger than
unsigned int on some platforms), and used the appropriate format.
MY_UCHAR_MAX and MY_USHORT_MAX are guaranteed to fit in an unsigned
int, so I used that rather than unsigned long, and of course
MY_UINT_MAX is already an unsigned int.

On one system, the revised program gave me the following output (other
systems may vary):

MY_SIZE_T_MAX 4294967295
MY_UCHAR_MAX 255
MY_USHORT_MAX 65535
MY_UINT_MAX 4294967295

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #15
Keith Thompson <ks***@mib.org> writes:
[...]
The printf function has know way of knowing the types of the arguments
you pass it; the only information it has is is the format string.
Getting the types right is entirely the caller's responsibility.

[...]

Argh. I should proofread before I post. Of course I meant,
"The printf function has know way of noing ...".

Or something like that.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #16
In <f1*************************@posting.google.com> le********@yahoo.com (Leon) writes:
Here is what I did,

#include <stdio.h>
#define SIZE_T_MAX ((size_t) -1)
#define UCHAR_MAX ((unsigned char) -1)
#define USHORT_MAX ((unsigned short) -1)
#define UINT_MAX ((unsigned int) -1)

int main()
{

printf("SIZE_T_MAX %d\n", SIZE_T_MAX);
printf("UCHAR_MAX %d\n", UCHAR_MAX);
printf("USHORT_MAX %d\n", USHORT_MAX);
printf("UINT_MAX %d\n", UINT_MAX);
}


I could have sworn that this is what you did, when I read your first post.
Never ever use a library function again before *carefully* reading its
specification.

UCHAR_MAX and USHORT_MAX are likely to get promoted to int, so %d is the
right thing and you get the expected results. Beware of 8 and 16-bit
compilers, where USHORT_MAX is going to be promoted to unsigned int,
though... So, to be on the safe side, convert USHORT_MAX to unsigned int
yourself and display it using %u instead of %d.

UINT_MAX will never be converted to int in this context and it is
highly unlikely that SIZE_T_MAX will.

For UINT_MAX, the solution is %u, since this is the correct conversion
descriptor for an unsigned int.

For SIZE_T_MAX things are a bit more complicated, because you don't
know its type and C89 doesn't provide any conversion descriptor that
is guaranteed to be the right thing for size_t. So, the C89 solution
is to use %lu and convert SIZE_T_MAX to unsigned long (the largest
unsigned type supported by the language). In C99, you can use the %zu
conversion descriptor for size_t values.

So, try:

printf("UINT_MAX %u\n", UINT_MAX);
printf("SIZE_T_MAX %lu\n", (unsigned long)SIZE_T_MAX);

and I bet you won't see any -1 nonsense!

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hanzhou.zhang | last post by:
Hi, vector<int> v; //... int size = v.size(); Since the vector's size() function returns a "size_t" number, the compilation with /Wp64 reports some warning. My question is: If I can...
11
by: javadesigner | last post by:
Hi: I am a bit new to C programming and am trying to write a wrapper around malloc. Since malloc takes size_t as it's parameter, I want to be able to see if my function recieved an argument...
18
by: Steffen Fiksdal | last post by:
Can somebody please give me some rules of thumb about when I should be using size_t instead of for example int ? Is size_t *always* typedef'd as the largest unsigned integral type on all systems...
11
by: Roka100 | last post by:
Hi, I am using size_t and ssize_t . But I am confused about them. <ssize_t> typedef int __ssize_t; typedef __ssize_t ssize_t; <size_t > typedef unsigned int size_t;
5
by: edware | last post by:
Hello, I have some questions about the size_t type. First, what do we know about size_t? From what I have read I believe that it is an unsigned integer, but not necessarily an int. Am I correct?...
39
by: Mark Odell | last post by:
I've always declared variables used as indexes into arrays to be of type 'size_t'. I have had it brought to my attention, recently, that size_t is used to indicate "a count of bytes" and that using...
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
22
by: subramanian100in | last post by:
Consider the following program #include <limits.h> #include <stddef.h> int main(void) { size_t size; size_t bytes = sizeof(size_t);
409
by: jacob navia | last post by:
I am trying to compile as much code in 64 bit mode as possible to test the 64 bit version of lcc-win. The problem appears now that size_t is now 64 bits. Fine. It has to be since there are...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.