C++ sizeof() Operator

In this tutorial, you will learn about the sizeof() operator in C++ with the help of examples.


The sizeof() is basically an operator which is used with primitive data types such as int, char, float, pointers, etc., to calculate the memory occupied by these data types in the system during the compilation of the code. The sizeof() operator calculates the memory of these data types in bytes.

The sizeof operator also can be used to get the size of classes, structures, unions, and any other user-defined data type

C++ Sizeof() operator syntax

The syntax of the sizeof() operator is as follows.

sizeof(data_type);

Here, sizeof is the keyword used, and data_type could be the data type of any variable used in the code.

C++ Sizeof() operator example

We will understand the working of the sizeof() operator with a few important scenarios. It will give you an in-depth overview of the operator.

1) If operand is of data type

#include <iostream> 
using namespace std; 
int main() 
{ 
// calculating the size of the primitive data type in bytes. 
std::cout << "Size of integer data type : " <<sizeof(int)<< std::endl; 
std::cout << "Size of double data type : " <<sizeof(double)<< std::endl; 
std::cout << "Size of float data type : " <<sizeof(float)<< std::endl; 
std::cout << "Size of char data type : " <<sizeof(char)<< std::endl; 
return 0; 
}

Here, we have calculated the size of primitive data types in bytes using the sizeof() operator. You can learn to implement the sizeof() operator from the above code. Since we know that int, double, float, and char carry 4,8,4 and 1 bytes, respectively. Below is the output of the program attached, and you can see that the same output is printed on the screen.

Output

Size of integer data type : 4
Size of double data type : 8
Size of float data type : 4
Size of char data type : 1

2) If the operand used is of class type

#include <iostream>
using namespace std;
class abc
{
    int x;
};
int main()
{
    abc y;
    std::cout << "Size of class abc is : " << sizeof(y) << std::endl;
    return 0;
}

Now, this is a great example. Here we have used an operand of class type instead of regular data types of variables in the sizeof() operator. We have created a class named abc. Inside the class is a variable named x of integer type. In the main function, we have created an object of class abc named y. Eventually, while printing the output, we used the sizeof() operator. Since there is only one variable inside the class abc, the size of the class will be equivalent to the size of the variable inside the class, which is 4 bytes as the int type takes 4 bytes of space.

Output

Size of class abc is : 4

3) If the operand used of array type

#include <iostream>
using namespace std;
int main()
{
    int arr[] = {5, 8, 10, 12};
    std::cout << "Size of the array 'arr' is : " << sizeof(arr) << std::endl;
    return 0;
}

In this case, we have used an operand of array type in sizeof() operator. In the main function, we have declared an array of int type name arr. The array contains four elements. If we do our calculation, then the output must be 16 as an integer takes 4 bytes of space, and the array contains four elements, so 4*4 = 16. That is precisely the output of the above program.

Output

Size of the array ‘arr’ is : 16

Conclusion

In this tutorial, you have learned about the sizeof() operator. With the help of the above examples, we are sure you must have understood all the key concepts. We now suggest you do some hands-on exercises on the same to test your knowledge and learnings from this tutorial.

Please get connected & share!

Advertisement