sizeof() operator in C++
The sizeof() is an operator that evaluates the size of data type, constants, variable. It is a compile-time operator as it returns the size of any variable or a constant at the compilation time.
The size, which is calculated by the sizeof() operator, is the amount of RAM occupied in the computer.
Syntax of the sizeof() operator is given below:
In the above syntax, the data_type can be the data type of the data, variables, constants, unions, structures, or any other user-defined data type.
The sizeof () operator can be applied to the following operand types:
If the parameter of a sizeof() operator contains the data type of a variable, then the sizeof() operator will return the size of the data type.
Let's understand this scenario through an example.
In the above program, we have evaluated the size of the in-built data types by using the sizeof() operator. As we know that int occupies 4 bytes, float occupies 4 bytes, double occupies 8 bytes, and char occupies 1 byte, and the same result is shown by the sizeof() operator as we can observe in the following output.
In the above program, we have evaluated the size of the class, which is having a single integer variable. The output would be 4 bytes as int variable occupies 4 bytes.
If we add one more integer variable in a class, then the code would look like:
In the above code, we have added one more integer variable. In this case, the size of the class would be 8 bytes as int variable occupies 4 bytes, so two integer variables occupy 8 bytes.
If we add a char variable in the above code, then the code would look like:
In the above code, the class has two integer variables, and one char variable. According to our calculation, the size of the class would be equal to 9 bytes (int+int+char), but this is wrong due to the concept of structure padding.
In the above program, we have declared an array of integer type which contains five elements. We have evaluated the size of the array by using sizeof() operator. According to our calculation, the size of the array should be 20 bytes as int data type occupies 4 bytes, and array contains 5 elements, so total memory space occupied by this array is 5*4 = 20 bytes. The same result has been shown by the sizeof() operator as we can observe in the following output.
Let's consider another scenario of an array.
In the above program, we have tried to print the size of the array using the function. In this case, we have created an array of type integer, and we pass the 'arr' to the function fun(). The fun() would return the size of the integer pointer, i.e., int*, and the size of the int* is 8 bytes in the 64-bit operating system.
In the above program, we have determined the size of pointers. The size of pointers would remain same for all the data types. If the computer has 32bit operating system, then the size of the pointer would be 4 bytes. If the computer has 64-bit operating system, then the size of the pointer would be 8 bytes. I am running this program on 64-bit, so the output would be 8 bytes. Now, if we provide the '*' symbol to the pointer, then the output depends on the data type, for example, *ptr1 is of integer type means the sizeof() operator will return 4 bytes as int data type occupies 4 bytes.
In the above program, we have declared two variables num1 and num2 of type int and double, respectively. The size of the int is 4 bytes, while the size of double is 8 bytes. The result would be the variable, which is of double type occupying 8 bytes.