When to use size_t?

Asked

Viewed 8,023 times

12

Already research on the subject, but I always end up getting confused. I found a article in English that explains very well some reasons for the existence of the type and how to use it. According to this article the type serves to represent sizes in bytes of objects, but always see in books and codes using the type size_t in several places that do not represent sizes.

So when should I use size_t and what advantages this use can bring?

4 answers

5

I use basically in 2 situations

1) sizes for allocation

size_t len;
// calcula len
ptr = malloc(len * sizeof *ptr);

2) for array index

for (size_t i = 0; i < sizeof array / sizeof *array; i++) {
    // work with array[i];
}
  • I make similar use. Generally, when I’m interacting with STL functions I use a lot size_t. To ensure that I am using the same type and avoid buying int and unsigned, for example.

3

According to this article the type serves to represent sizes in bytes of objects

Most correct.

The answers (up to the time of posting this one) are mistaken when they say that size_t is equivalent to the primitive unsigned int.

First, the relevant paragraph of the standardisation is the following:

[support.types.layout]/3

The type size_­t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any Object ([expr.sizeof]).

The emphases (mine) clearly say: size_t is an entire type without signal. In other words, size_t may well be equivalent to uint8_t, and may also be equivalent to uint64_t, or other types of similar characteristic. Therefore, size_t is not guaranteed to be equivalent to unsigned int. It’s allowed, yes, but it’s different than being guaranteed.

Back to the question:

[...] but I always see in books and codes using the size_t type in several places that do not represent sizes.

size_t is usually used to index arrangements (arrays) and count loops (loops). Why? Let’s use an example: when using a different type, such as unsigned int, to index an array, it is possible that the indexing operation will fail in 64-bit systems when the index exceeds the value of UINT_MAX, or whether the index depends on modular arithmetic to return to zero after incrementing the maximum index 1.

But still, the right kind to index containers (std::vector, std::string etc) is always the member ::size_type, a typedef that is given by such containers (std::vector<int>::size_type for example). Normally, however, ::size_type is defined as synonymous with size_t 1. The member functions size() containers also return a ::size_type.

By contrast, the type ptrdiff_t can also be used for arrangement indexing, although it is directed to the type of pointer difference value (and ::difference_type for iterator difference) 2.

1


Actually there is not an exact moment that you should use size_t. Because it is the same as 'unsigned int' you ultilize it anywhere you would use an 'unsigned int', the only difference is that an 'unsigned int' is more suitable for sizes, because it is an unsigned, all its values that would be negative go to the space of the positive values which is better since practically it does not measure size in bytes with negative values. And the advantage of using the int type is that byte sizes are measured in integer numbers, not decimals, so it would be useless to use float or double.

size_t was made more specifically to be used as a result of the sizeof operator, but it is usually also used for indexing arrays and loop counter(for, while) which are operations that usually do not use negative or decimal values.

So basically, the advantage of using size_t is the same as using the 'unsigned int' in the appropriate location, but as a matter of organization, size_t is more used to represent sizes, after all, it would make no sense to use itlo for other purposes if we can use the 'uint32_t' which represents the same thing.

  • std::size_t is not equivalent to unsigned int according to the specification: http://www.eel.is/c++draft/support.types.layout#3

  • Hello @Márioferoldi, Apparently link you quoted is broken. Anyway, I do not know you came to consult more sources, but apparently since the old standards of C like ISO/IEC 9899 (C99) link, in most sources found on the internet and even in the GCC source code link the size_t is treated as a unsigned int.

  • I was able to access the cited link, and apparently there is stating the same thing that was answered. " The type size_-t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any Object", i.e., confirms that size_t is a defined implementation of unsigned int

  • 1

    Most wrong. What the implementation does (GCC in this case) is not relevant to standardization. "Unsigned integer" is any kind unsigned whole, I mean, it might as well be uint8_t, uint16_t, uint32_t, or uint64_t. This is easily proved empirically if you test sizeof(size_t) == sizeof(unsigned int) where the implementation defines size_t as unsigned long long (e.g., most 64 bit architectures).

  • Please correct your answer.

-2

size_t is a typedef for unsigned int. So any situation that fits your use, is valid and you can use. Of course by observing the ceilings for the situation.

Historically, this definition was made for indexing vectors, loops as well as for the result of using the sizeof operator.

However, it can be understood as any type and used without problem in any situation that is plausible.

  • There is nowhere in the specification saying that size_t is synonymous with unsigned int.

  • @Márioferoldi, here is the setting, it is in the stddef header. h.

  • No, this is an implementation detail, not what the specification defines. In the specification, std::size_t must be able to store the maximum size of any object of any type (realizes which lines after GCC defines how unsigned long). In 32bit systems, for example, std::size_t can have 4 bytes (assuming 1 byte = 8 bits), and on 64bit systems, std::size_t can have 8 bytes. But all this is implementation detail, what really is relevant is what the specification says.

  • And here is the relevant paragraph: http://www.eel.is/c++draft/support.types.layout#3

  • Quiet, it has the definition but it undergoes a slight variation depending on the architecture on which the program will be compiled and run. The specification suggests, but implementations vary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.