Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why size_t exists in C/C++ and could/should it be replaced?

I'm an electrical engineer turned to computer scientist. It's really hard for me to understand why in C++ there are so many things that are almost the same but not completely the same. An example is short vs int vs unsigned int vs size_t vs long int vs long long int vs uint8_t (I don't know if there is any additional way to designate an integer). It seems that it makes the language unnecessarily complicated.

Could or should size_t be replaced or does it have any feature impossible to use in another way?


[EDIT]

After the helpful answers, there is something that I still don't fully see. size_t is useful in terms of portability and performance as several persons suggested. But how helpful, is there a quantitative way or numerical evidence to measure the advantages over having just only int and retire all its brothers???

like image 599
hoaphumanoid Avatar asked Jan 01 '16 19:01

hoaphumanoid


People also ask

Why do we need size_t in C?

The datatype size_t is unsigned integral type. It represents the size of any object in bytes and returned by sizeof operator. It is used for array indexing and counting. It can never be negative.

Should you use size_t in C?

Using size_t appropriately can improve the portability, efficiency, or readability of your code. Maybe even all three. Numerous functions in the Standard C library accept arguments or return values that represent object sizes in bytes.

Should I always use size_t?

The reason why size_t is encouraged is because it makes your program portable. Typically a program should not need to care how much memory is installed in the machine it is running on. But if the program logic dictates that there will be a lower limit on the number of elements then you could use a smaller index type.

Should I use size_t instead of int?

If we consider the standard, both are integers of size 16 bits. On a typical 64-bit system, the size_t will be 64-bit, but unsigned int will be 32 bit. So we cannot use them interchangeably. One standard recommendation is that the size_t be at most as big as an unsigned long.


2 Answers

std::size_t is a typedef for an unsigned fundamental type, and it should be able to represent any possible index/size in your program (technically, it is also the result of the sizeof operator). Its underlying fundamental type may differ on different implementations, and because you want portability, you use std::size_t and don't care anymore whether it's a unsigned long int or unsigned long long int etc.

std::size_t does not use any feature impossible to use in another way, it is just a convenient type alias and nothing more.

In response to the OP edit

@HoapHumanoid The edit is not a very good question for StackOverflow, as it is a matter of personal interpretation/preference. Of course you can have only one numeric type, or fixed-size types, however when you want to squeeze as much performance out of your processor, you better have many, each capable of representing a specific range, depending on the physical platform. Because computer architectures are vastly different, each architecture will impose its own size for e.g. int, long etc. When you combine this issue with portability, then size_t emerges naturally. What else would you use in a generic function that e.g. is guarantee to allocate as much memory as possible on every possible implementation? Would you use int to pass the number of bytes? Or maybe long int? You need to read the compiler manual, check the appropriate type etc, then use it. That's what the C++ implementation does for you, defines an appropriate such type.

like image 62
vsoftco Avatar answered Sep 21 '22 14:09

vsoftco


You forgot long long int, most of the unsigned versions, uint8_t and friends, and probably some more. :-)

In some languages the size of the integer types is fixed. C++ is not one of those; lots of flexibility is given to the programmer to allow balancing performance against size and range.

But size_t is extremely useful. It is guaranteed to be able to hold the size of any object, or any valid array index. Not having it would make writing portable yet efficient programs much more difficult.

like image 36
Alan Stokes Avatar answered Sep 21 '22 14:09

Alan Stokes