Difference between size_t and unsigned int?

I am so confused about size_t . I searched on the Internet and mentioned everywhere that size_t is an unsigned type, so it can only represent non-negative values.

My first question is: if it is used to represent only non-negative values, why not use unsigned int instead of size_t ?

My second question is: are interchangeable size_t and unsigned int or not? If not, why?

And can anyone give me a good example of size_t and briefly its work?

+74
c ++ c types unsigned
Nov 01 '13 at 17:47
source share
7 answers

if it is used to represent a non-negative value, so why don't we use unsigned int instead of size_t

Because unsigned int is not the only unsigned int integer type. size_t can be any of unsigned char , unsigned short , unsigned int , unsigned long or unsigned long long , depending on the implementation.

The second question is that size_t and unsigned int interchangeable or not, and if not, why not?

They are not interchangeable for the reason described above ^^ .

And can anyone give me a good example of size_t and its concise work?

I do not quite understand what you mean by "his brief work." It works like any other unsigned type (in particular, like the type to which it refers). You are advised to use size_t when you describe the size of an object. In particular, the sizeof operator and various standard library functions such as strlen() return size_t .

Bonus: here's a good article on size_t (and the closely related type ptrdiff_t ). This very well explains why you should use it.

+67
Nov 01 '13 at 17:52
source share

In C:

There are 5 standard unsigned integer types:
  • unsigned char
  • unsigned short
  • unsigned int
  • unsigned long
  • unsigned long long

with different requirements for their sizes and ranges (in short, each type range is a subset of the next type, but some of them may have the same range).

size_t is a typedef (i.e. an alias) for some unsigned type (perhaps one of the above, but possibly an unsigned extended integer type, although this is unlikely). This is the type specified by the sizeof operator.

On one system, it might make sense to use unsigned int to represent sizes; on the other hand, it makes sense to use unsigned long or unsigned long long . ( size_t unlikely to be either unsigned char or unsigned short , but this is allowed).

The purpose of size_t is to save the programmer from having to worry about which of the predefined types is used to represent sizes.

Code that assumes sizeof gives an unsigned int will not be portable. Code that assumes that it gives size_t will most likely be portable.

+55
Nov 01 '13 at 17:52
source share

size_t has a special limitation.

Quote from http://www.cplusplus.com/reference/cstring/size_t/ :

An alias of one of the fundamental unsigned integer types.

This is a type that can represent the size of any object in bytes : size_t is the type returned by the sizeof operator and is widely used in the standard library to represent sizes and counts.

It is not interchangeable with unsigned int , because the size of the int is specified by the data model. For example, LLP64 uses a 32-bit int , and ILP64 uses a 64-bit int .

+8
Nov 01 '13 at 17:51
source share

size_t is used to store the sizes of data objects and is guaranteed to contain the size of any data object that a specific implementation of C can create. This data type can be smaller (in the number of bits), larger or exactly the same as an unsigned int.

+5
Nov 01 '13 at 17:51
source share

size_t type is a basic unsigned integer type in C / C ++. This is the type of result returned by the sizeof operator. The font size is chosen so that it can store the maximum size of a theoretically possible array of any type. On a 32-bit system, size_t will accept 32 bits, on a 64-bit, one bit is 64 bits. In other words, the variable size_t can safely hold a pointer. The exception is pointers to class functions, but this is a special case. Although size_t can hold a pointer, it is better to use another unsigned integer type uintptr_t for this purpose (its name reflects its capabilities). The types size_t and uintptr_t are synonyms. size_t - this is commonly used for loop counters, array indexing, and address arithmetic. The maximum possible value of type size_t is constant SIZE_MAX.

+2
Nov 01 '13 at 19:00
source share

Among other answers, it also documents the code and tells people that you are talking about the size of objects in memory

+2
Oct 31 '14 at 20:06
source share

Simply put, size_t is platform dependent and implementation dependent, while unsigned int depends only on the platform.

0
Feb 25 '19 at 14:45
source share



All Articles