I'm confused about size_t
. I know it's an unsigned type..right?
My question is, when should it be used. Is there a reason why it should be used with a regular array? I mean one would have to declare the array size to be really huge, so huge that a regular unsigned or signed wouldn't be able to handle it. And then a size_t
would be able to deal with it right? Can someone give me an example?
size_t is commonly used for array indexing and loop counting. Programs that use other types, such as unsigned int, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX or if it relies on 32-bit modular arithmetic.
The reason why size_t is encouraged is because it makes your program portable. Typically a program should not need to care how much memory is installed in the machine it is running on. But if the program logic dictates that there will be a lower limit on the number of elements then you could use a smaller index type.
When writing C code you should always use size_t whenever dealing with memory ranges. The int type on the other hand is basically defined as the size of the (signed) integer value that the host machine can use to most efficiently perform integer arithmetic.
The only time that using std::size_t is "better" than ::size_t is if you have not included <stddef. h> (perhaps you have #include <cstddef> instead).
According to ISO IEC 14882:2011(E)
§ 18.2 6 The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.
That makes std::size_t
the natural choice when writing library code that deals with dynamic allocation, particularly with arrays and containers that manage arrays.
However in your own code, if you know your array is never going to be larger than 15-20 elements (for example) then there is no need to work with std::size_t
wide values if you prefer to use something smaller.
Do not use unsigned
for size/index unless you're forced to for some reason. In C/C++ unsigned
does NOT mean non-negative
.
The face that size_t
is an unsigned
type in the language and standard library is a design mistake explainable only with the historical context of the time when the decision was taken (CPUs were 16-bit back then but RAM was getting large).
See
https://stackoverflow.com/a/3260179/320726 https://stackoverflow.com/a/24104825/320726 https://stackoverflow.com/a/18248537/320726 https://stackoverflow.com/a/31089063/320726 https://stackoverflow.com/a/30799874/320726 https://stackoverflow.com/a/3029941/320726
for more details and examples of bugs that using an unsigned type to represent a quantity may introduce...
PS: Unfortunately for reasons that are not clear to me, saying something totally obvious like that the difference of two non-negative values can be negative is seen as a personal offence by a curiously high percentage of C/C++ "experts". If you like to be with the majority more than you like to be right then probably jumping on the "size_t
being unsigned is the best idea since sliced bread" bandwagon is a viable option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With