size_t vs int

I've come across this multiple times. More than one article has been made on the topic (such as https://ewontfix.com/9/ ).

The size_t type isn't defined in standard. Nor is the int type. That doesn't mean they're the same in any way. int usually means 32-bit on most systems (although it may mean 64-bit). size_t usually depends on architecture, being 32-bit on x86 and 64-bit on amd64.

On a 32-bit platform, I've seen people cast from size_t to int. This is wrong on various levels. You can't know if they're the same size. On a 32-bit system, they normally are the same. However, that same code on a 64-bit platform suddenly breaks or (hopefully) doesn't compile.

gcc, vc++, and clang do not warn about doing this either. if sizeof(int) == sizeof(size_t), they'll let it by despite it being potentially undefined behavior. However, on a 64-bit system, these compilers won't allow it. When casting from size_t to int, it will warn. when passing a size_t type to a function that expects an int type, it will give an obscure error saying there is no matching function.

On top of *all* of this, size_t is unsigned, int is signed. They aren't even represented the same way so I can't begin to fathom why the fuck gcc doesn't warn at least on this. http://cpp.sh/7qs3

[/rant]
Last edited on
closed account (48T7M4Gy)
http://en.cppreference.com/w/cpp/types/size_t
Hi,

Just wondering if -Wconversion works for you, it's one of the options not turned on despite -Wall -Wextra -pedantic-errors

-Wconversion
Warn for implicit conversions that may alter a value. This includes conversions between real and integer, like abs (x) when x is double; conversions between signed and unsigned, like unsigned ui = -1; and conversions to smaller types, like sqrtf (M_PI). Do not warn for explicit casts like abs ((int) x) and ui = (unsigned) -1, or if the value is not changed by the conversion like in abs (2.0). Warnings about conversions between signed and unsigned integers can be disabled by using -Wno-sign-conversion.

For C++, also warn for confusing overload resolution for user-defined conversions; and conversions that never use a type conversion operator: conversions to void, the same type, a base class or a reference to them. Warnings about conversions between signed and unsigned integers are disabled by default in C++ unless -Wsign-conversion is explicitly enabled.
This has been a discussion point for ages.
More reading for interest:

Should I use Signed or Unsigned ints? by Robert Elder
http://blog.robertelder.org/signed-or-unsigned/

Signed versus unsigned integers by Nigel Jones
http://embeddedgurus.com/stack-overflow/2009/05/signed-versus-unsigned-integers/

Pitfalls in C and C++: Unsigned types by Chris Cannam
http://www.soundsoftware.ac.uk/c-pitfall-unsigned
(I disagree with his examples -- a broken piece of code does not mean unsigned is evil -- the programmer simply screwed up. This is, however, a good argument for the problems with signed vs unsigned values.)

Why is unsigned integer overflow defined behavior but signed integer overflow isn't? (Answer by Pascal Cuoq)
http://stackoverflow.com/a/18195756/2706707

signed vs unsigned int for indexes and sizes on NVIDIA's forums
https://devtalk.nvidia.com/default/topic/796841/signed-vs-unsigned-int-for-indexes-and-sizes/

Well, 'nuff for now.
Topic archived. No new replies allowed.