C ++ - should you size_t with a regular array?

I am confused about size_t . I know this is 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, it would be necessary to declare that the size of the array will be really huge, so huge that a regular unsigned or signed one will not be able to handle it. And then size_t will be able to handle this correctly? Can someone give me an example?

+6
source share
4 answers

According to ISO IEC 14882: 2011 (E)

Β§ 18.2 6 The size_t type is an unsigned integer type defined for implementation that is large enough to contain the size in bytes of any object.

This makes std::size_t natural choice when writing library code that deals with dynamic allocation, especially arrays and containers that manage arrays.

However, in your own code, if you know that your array will never be more 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.

+4
source

TL DR

Do not use unsigned for size / index if you are not forced for some reason. In C / C ++, unsigned does NOT mean non-negative .

The person that size_t is an unsigned type in the language and standard library is a design error , explained only by the historical context of the time when the decision was made (the processors were 16-bit back, but the RAM was getting big).

Cm

fooobar.com/questions/43648 / ... fooobar.com/questions/154648 / ... fooobar.com/questions/84176 / ... fooobar.com/questions/992586 / ... fooobar.com/questions/ 992587 / ... fooobar.com/questions/741371 / ...

for more details and examples of errors that, using the unsigned type to represent quantities, can introduce ...

PS . Unfortunately, for reasons that are incomprehensible to me, the statement is something quite obvious, since the difference in two non-negative values ​​can be negative, is considered as a personal violation with a curiously high percentage of C / C ++ experts. If you enjoy being with the majority more than you enjoy being right, then probably jumping to β€œ size_t , which is unsigned, is a better idea, as sliced ​​bread” is the winner.

+2
source

size_t is an abstraction. You do not have to worry about what primitive type it displays. Each container has its own type size_type , which is usually used to convey the size of the container. In most cases, this is the typedef of the dispenser class used in the container. Many containers use unsigned primitives, but I have seen many external libraries just using signed int as their size_type . This is simply an attempt to standardize the size types used in containers. The size_t target should not be used with its own array. It is designed to return the size of the container or anything that has a "size". You certainly do not want the "native" array to be so large that it has more elements than the maximum int value

+1
source

A lot of very good answers ... But keeping it simple

According to ISO IEC 14882: 2011 (E)

Β§ 18.2 6 The size_t type is an unsigned integer type defined for implementation that is large enough to contain the size in bytes of any object.

Use it if you know for sure that you are dealing with whole whole signs. For the most part, the array indices correspond to the count.

Secondly, it is useful when the code should be a neutral platform. As on a 16-bit processor versus a 32-bit and a 64-bit processor, an unsigned integer will have a different size and, therefore, it is better to use size_t. Consequently, it is widely used in libraries.

+1
source

All Articles