Tutorial Study Image

Sizeof() operators in C++


September 20, 2022, Learn eTutorial
1236

The sizeof() operator in C++ will be covered in this section. sizeof() is a compile-time operator that can be used mainly to calculate the size of variables or any other user-defined or predefined datatypes.

What do you mean by the size of() operators in C++?

The sizeof() operator measures the size of variables, constants, as well as data types. As it returns the size of any variable or the size of the constant at compilation time, it is a kind of compile-time operator. The size is the amount of RAM used by the computer and is determined by the sizeof() operation.

Sometimes a user will wish to know how much memory the compiler has allocated, therefore for this purpose C++ designers are provided with the sizeof(data) keyword.  The sizeof() operator returns a value or the result in integer format and also in terms of bytes.

Syntax for the sizeof() operator 


sizeof(data_type);  
 

The data type in the syntax which is mentioned above can be a user-defined data type, a variable, a constant, a union, a structure, or it can even be the data type of the data.

The sizeof () operator can be applied mainly with the following operand types:

  1. Whenever an operand is of  a data type
  2. Whenever an operand is of a class type
  3. Whenever an operand  is of an array type
  4. Whenever an operand is of a pointer type

Whenever an operand is of  a data type

The sizeof() operator will surely return the size of the data type if and only if the parameter contains the data type of the variable.

For example :

By utilizing the sizeof() operator, we evaluated the size of the in-built data types in the above-mentioned program. As we already know, an int takes up four bytes, a float takes up four bytes, a double takes up eight bytes, and a char takes up one byte. The sizeof() operator gives the same answer, as we can see in the output of the above program.

Whenever an operand is of a class type

What happens when an operand is of a class type? let us understand this with the help of a simple example.


#include <iostream>

using namespace std;
class Boss {
    int a;
};
int main() {
    Boss b;
    std::cout << "The size of class Boss will be  : " << sizeof(b) << std::endl;
    return 0;
}

Output:

The size of class Boss will be: 4

In the preceding program, we determined the size of the class, which has a single integer variable. Since the int variable takes up 4 bytes, the result would be 4 bytes.

Suppose, if we are  adding just  one more integer variable to a class then  the code would look like this:


#include <iostream>

using namespace std;
class Boss {
    int a;
    int d;
};
int main() {
    Boss b;
    std::cout << "The size of class Boss will be  : " << sizeof(b) << std::endl;
    return 0;
}

Output:


The size of class Boss will be  : 8

One more integer variable has been added to the code above. In this scenario, the class would be 8 bytes long because an int variable takes up 4 bytes and two integer variables take up 8 bytes.

Suppose, if we are adding char variable to the same code then our code would look like this:


#include <iostream>

using namespace std;

class Boss {
    int a;
    int d;
    char ch;
};
int main() {
    Boss b;
    std::cout << "The size of class boss will be : " << sizeof(b) << std::endl;
    return 0;
}

Output:


The size of class boss will be : 12

Two integer variables and one char variable belong to the class in the code above. Our calculations would have the class taking up 9 bytes (int+int+char), however, this is incorrect because of the idea of structure padding.

3. Whenever an operand is of an array type

What happens when an operand is of an array type? let us understand this with the help of a simple example.


#include <iostream>  
using namespace std;  
 int main()  
{  
  int arr[]={40,50,60,70,80};  
  std::cout << "The size of the array 'arr'will be : "<<sizeof(arr) << std::endl;  
  return 0;  
}

Output:

The size of the array 'arr'will be : 20

We have declared an integer-type array with five elements in the program above. We determined the array's size using the sizeof() operator. The array should be 20 bytes in size, as the int data type takes up 4 bytes and there are 5 entries in the array, making a total memory space need of 5 * 4 = 20 bytes. As we can see in the output below, the sizeof() operation has produced the same result.

Now let us try to print the size of an array 


#include <iostream>  
using namespace std;  
void fun(int arr[])  
{  
    std::cout << " The size of the array will be : " <<sizeof(arr)<< std::endl;  
}  
int main()  
{  
  int arr[]={40,50,60,70,80};  
  fun(arr);  
  return 0;  
} 

Output:

The size of the array will be : 8

We attempted to use the function in the above program to output the array's size. In this scenario, we built an array that is of the type integer and passed the 'arr' to the function fun (). The fun() function will return mainly the size of the integer pointer, that is, int*, and the size of int* in the 64-bit operating system is 8 bytes.

4.    Whenever an operand is of a pointer type

What happens when an operand is of a pointer type? let us understand this with the help of a simple example.

We determined the size of the pointers in the preceding program. The pointer size would be the same for all the data types. The pointer would be 4 bytes in size if the computer had a 32-bit operating system. The pointer would be 8 bytes in size if the computer had a 64-bit operating system. Since I am using 64-bit for my software, the result would be 8 bytes. Now, if we give the pointer the symbol "*," the output will change depending on the data type. For example, if *ptr1 is of the integer data type, the sizeof() operator will return 4 bytes since the int data type uses  4 bytes.

5.    Whenever an operand is of an expression

What happens when an operand is of an expression? let us understand this with the help of a simple example.

#include<iostream>  
using namespace std;  
   
int main()  
{  
   int num1;  
   double num2;  
   cout <<sizeof(num1+num2);  
     return 0;  
} 

Output:

8

We have mainly declared two variables, num1 as well as num2, of types int and double, respectively, in the program which is mentioned above. Integers have a size of 4 bytes, but doubles have an actual size of 8 bytes. The variable is of double type and occupies 8 bytes as a result.