Is size_t the same as an unsigned int? Thanks.
Is size_t the same as an unsigned int? Thanks.
It might be (the same size). It doesn't have to be.
Quzah.
Hope is the first step on the road to disappointment.
It is an implementation defined value that typically is an unsigned word on the machine, but it is at the compiler writer's whim (well, probably not, but close enough) as to what the data type is.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
What about situations like these? Should you declare i as size_t instead of int or unsigned int?
Code:char *s = "Some string"; int i; /* Should you use size_t i here? */ size_t len = strlen(s); for (i=0; i<len; i++) { ; /* Do something */ }
To be absolutely safe you should always follow the documentation for return types. You can get some real subtle bugs (even ones that can allow crackers to take over your program) if you use the wrong type.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
> int i; /* Should you use size_t i here? */
Yes.
If you'd done
int len = strlen(s);
You'd have the conversion on the assignment instead of the comparison.
The problem is still there, you just moved it.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
> It is an implementation defined value that typically is an unsigned word on the machine, but it is at the compiler writer's whim (well, probably not, but close enough) as to what the data type is.
Is size_t guaranteed to be unsigned? I've read in several places that the ISO C standard says it must be unsigned, but I just wanted to make sure. I've seen lots of code that uses (size_t)(-1) to get the max value, and if size_t isn't unsigned then well...
>Is size_t guaranteed to be unsigned?
Yes. The standard says that size_t is an "unsigned integer type".
>I've seen lots of code that uses (size_t)(-1) to get the max value, and if size_t isn't unsigned then well...
There's a difference between -1 and -1 cast to size_t. Do a little test that prints -1 as a signed value, then as an unsigned value.
My best code is written with the delete key.
> There's a difference between -1 and -1 cast to size_t. Do a little test that prints -1 as a signed value, then as an unsigned value.
I know, that's why I wrote "to get the max value".
The max/min values of data types are in a header file, limits.h I think.
I don't think the max value of size_t is defined anywhere. But you can easily get it by doing this cast anyway (size_t)(-1).
>I don't think the max value of size_t is defined anywhere.
C99 adds SIZE_MAX in the stdint.h header.
My best code is written with the delete key.
Oh that's good to know.