C++ size_t

The size_t is an acronym for an unsigned integer data type. It is indeed the kind generated either by sizeof() function and return type for the different string function to get lengths. It is commonly used throughout the standard library to express lengths and counts. The required length of a hypothetically feasible entity of any kind can be stored in std::size_t (including array). An inadequately kind is the one whose length couldn’t be expressed by std::size_t. For array addressing and looping counts, std::size_t is often used. Therefore, we have been starting this article with the C++ new file generation using the “touch” instruction of the Ubuntu shell. Start coding your examples in the file by launching the newly created file with the “gnu nano” editor.

Example 01:

Let’s have a fresh start with the simple example of using “size_t” in the code. We have to start this code after opening the file with the “iostream” library at the first line with the “#include” keyword of C++. The “std” namespace of C++ has been added to get help for using the standard cin and cout statements in the code. We have defined a variable “n” with a value of 10 that will be used further in the code as a size. Within the main() function, we have defined an integer array named “var” of size “n”. To perform array indexing and iteration counting, we use the size_t within the “for” loop. It is because when we use some unsigned integer to loop out some array, it may sometimes cause an error on a 64-bit Linux machine.

The loop has been started from 0 to size “n” and pre-incrementing the size_t type variable “I”. This variable “I” has been used here to perform array indexing or adding values to the array “var”. The same index number will be a value to that particular index, i.e., its value would be the same. The cout statement shows the value at the same index number. After the line, we have given a one-line break using the “endl” keyword within the cout statement. The code is completed now and ready to be cast off in the shell.

So, we need to make sure that the code is compiled with a g++ compiler before its execution. We finally compiled our code and executed it with the “./a.out” command of Ubuntu 20.04. The loop has been started from index 0 and goes up to 9th index and added the same values to the array “var”. The output shows the array values in sequence by its indexes.

Example 02:

We know the size of any variable can never be a negative number. Therefore, size_T will cause an infinite loop and segmentation fault error in the code while used within the decremented loop. So, we have started it with an iostream header and standard “std” namespace. The same variable “n” is defined with a value of 10. Within the main() function, the same integer type of an array “var” is defined with the size “n”. Now, the “for” loop has been using the “size_t” member in it to iterate the loop starting from size “n-1” and it goes up to 0 or greater than 0. Each time the value of a variable “I” will be decremented. The cout standard statement is here to display the value at each index. The program ended here.

The infinite loop and segmented core fault will be displayed on the execution of this code.

Example 03:

Let’s see how a “size_t” can be differentiated from other types. After the namespace and header, we have started our code with simple two cout statements. The cout statements have been checking the size of “int” and “size_t” types separately with the use of the sizeof() function. Let’s just save this program and make it execute on the shell to see what happens.

The code should be compiled with g++, as shown below. After that, it will be executed with the “./a.out” command within the Ubuntu terminal. The output shows the size of the “int” type is 4, and the size of “size_t” is 8. It shows that the size_t stores a large amount of data in it as compared to the “int” type.

Example 04:

Within this C++ illustration, we will take a look at how we can check the sizeof() array variables of int and size_t type. The code has been started with three main headers, i.e. cstddef, iostream, and array. The main() method is started with the declaration of an integer array of size 100. The actual size has been got from the sizeof() function on this array and saved to the variable s1. The cout is here to display this size on the shell. Now another array “A2” of size_t type has been initialized with size 100. The actual size of this array has been found out with the “sizeof” function and saved to the variable s2. The cout is here again to display it on the console.

The code compilation and execution came up with the below output. We can see that the length of the size_t type array is double the size of the int type array.

Example 05:

Let’s have another example to see how much the maximum size can be used for a variable. The header files and “std” namespace are the same as above. Within the main() function, we have to use the cout statement along with the built-in SIZE_MAX of C++. Let’s save this code now.

We have the maximum size we can use for our system on executing this code.

Let’s update the code a little. So, we have declared an integer array variable of large size. The size of this array has been found out with the sizeof() function and saved to variable “s” of size_t type. The cout statement comes up again to display the size we got from the “s” variable. The “if” statement of C++ is here to check if the size “s” we have got is greater than the maximum size our system ahllows or not. If so, it will display the message using the cout clause that the maximum size must not exceed the specified one. Let’s save and execute the code.

After the code execution, the below output has been displayed on the screen. It shows some warnings upon the compilation. The execution shows the size of variable “A” and displays the message that the maximum size must not exceed the particular size.


Finally! We have explained the size_t data member with some very simple and easy-to-do examples. We have discovered the use of size_t type within “for” loops in increment or decrement order of sequence. We have utilized the sizeof() function to see the size of size_t and int type variables in the code. We have also seen how much size a 64-bit system can allow us to use for variables and how to find it out. Thus, we are extremely sure that this article has all the necessary information regarding size_t type and its uses.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.