Can someone explain why the type size_t is used with an example?

I was wondering why this size_t used where I can use say int type. He said that size_t is a return type of the sizeof operator. What does it mean? for example, if I use sizeof(int) and save what returns to a variable of type int , then it also works, there is no need to store it in a variable of type size_t . I just want to know the basic concept of using size_t with a clear example. thanks

+7
source share
7 answers

size_t guaranteed to be able to represent the largest possible size, int - not. This means that size_t more portable.

For example, what if an int can only store up to 255, but could you allocate arrays of 5000 bytes? Obviously this will not work, however with size_t it will.

+10
source

The simplest example is quite dated: on an old 16-bit int system with 64K of RAM, the int value can be from -32768 to +32767, but after:

 char buf[40960]; 

the buf buffer is 40 kilobytes, so sizeof buf too large to fit int , and it needs unsigned int .

The same thing can happen today if you use a 32-bit int , but you allow programs to access more than 4 GB of RAM at a time, as is the case with the so-called "I32LP64" (32 bits int , 64-bit long and pointer). Here the size_t type will have the same range as the unsigned long .

+4
source

You use size_t mainly for cast pointers to unsigned integers of the same size to perform calculations on pointers as if they were integers that would otherwise be prevented at compile time. Such code is intended to compile and build correctly in the context of different pointer sizes, for example. 32-bit model versus 64-bit version.

+1
source

The implementation is defined, but on 64-bit systems you will find that size_t often takes up 64 bits, and int is still 32 bits (if only ILP64 or SILP64 ).

0
source

depending on the architecture you are in (16-bit, 32-bit, or 64-bit), the int may be of a different size.

if you want a specific size to use uint16_t or uint32_t .... you can check this thread for more information

What does the C ++ standard mean for int size, long type?

0
source

size_t is a typedef defined to store the size of an object. It can store the maximum object size that is supported by the target platform. This makes it portable.

For example:

 void * memcpy(void * destination, const void * source, size_t num); 

memcpy () copies num bytes from source to destination. The maximum number of bytes that can be copied depends on the platform. Thus, doing num as type size_t makes memcpy portable.

See https://stackoverflow.com/a/176189/ for more information.

0
source
  1. size_t is a typedef for one of the basic unsigned integer types. This can be unsigned int, unsigned long or unsigned long long depending on the implementation.
  2. Its special property is that it can represent the size (in bytes) of any object (which also includes the maximum possible object!). This is one of the reasons it is widely used in the standard library for indexing arrays and counting loops (which also solves the portability problem). Let me illustrate this with a simple example.

Consider a vector of length 2 * UINT_MAX, where UINT_MAX denotes the maximum value of an unsigned integer (for my implementation, this is 4294967295, taking into account 4 bytes for an unsigned integer).

std :: vector vec (2 * UINT_MAX, 0);

If you want to fill a vector with a for loop such as this one, it will not work, since unsigned int can iterate only to the point UINT_MAX (after which it will start again from 0).

for (unsigned int i = 0; i <2 * UINT_MAX; ++ i) vec [i] = i;

The solution here is to use size_t, since it is guaranteed to represent the size of any object (and therefore our vector vec!) In bytes. Please note that for my implementation, size_t is a typedef for unsigned long, and therefore its maximum value = ULONG_MAX = 18446744073709551615 considering 8 bytes.

for (size_t i = 0; i <2 * UINT_MAX; ++ i) vec [i] = i;

Recommendations: https://en.cppreference.com/w/cpp/types/size_t

0
source

All Articles