简体   繁体   中英

can anyone explain why size_t type is used with an example?

I was wondering why this size_t is used where I can use say int type. Its said that size_t is a return type of sizeof operator. What does it mean? like if I use sizeof(int) and store what its return to an int type variable, then it also works, it's not necessary to store it in a size_t type variable. I just clearly want to know the basic concept of using size_t with a clearly understandable example.Thanks

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

For instance, what if int could only store up to 255 but you could allocate arrays of 5000 bytes? Clearly this wouldn't work, however with size_t it will.

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

char buf[40960];

the buffer buf occupies 40 kbytes, so sizeof buf is too big to fit in an int , and it needs an unsigned int .

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

You use size_t mostly for casting pointers into 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, eg 32-bit model versus 64-bit.

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

For example:

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

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

Refer https://stackoverflow.com/a/7706240/2820412 for further details.

  1. size_t is a typedef for one of the fundamental unsigned integer types. It could be unsigned int, unsigned long, or unsigned long long depending on the implementation.
  2. Its special property is that it can represent the size of (in bytes) of any object (which includes the largest object possible as well!). That is one of the reasons it is widely used in the standard library for array indexing and loop counting (that also solves the portability issue). Let me illustrate this with a simple example.

Consider a vector of length 2*UINT_MAX, where UINT_MAX denotes the maximum value of unsigned int (which is 4294967295 for my implementation considering 4 bytes for unsigned int).

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

If you would want to fill the vector using a for-loop such as this, it would not work because unsigned int can iterate only upto the point UINT_MAX (beyond 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 too!) in bytes. Note that for my implementation size_t is a typedef for unsigned long and therefore its max value = ULONG_MAX = 18446744073709551615 considering 8 bytes.

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

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

它是实现定义的,但是在64位系统上,您会发现size_t通常为64位,而int仍为32位(除非是ILP64或SILP64模型)。

depending on what architecture you are on (16-bit, 32-bit or 64-bit) an int could be a different size.

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

What does the C++ standard state the size of int, long type to be?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM