C++

C++ Sizeof()

The sizeof() is an operator in C++. It is a unary operator that helps the programmers determine the size of the operands that they are using. An integral type, typically denoted by size “t”, is the outcome of this operator. This operator is typically used with primitive data types such as integer, float, pointer, etc. Additionally, it may provide the size of a more sophisticated data types like unions and structures. It is an operator that can calculate the size of the operand and determine the size of any data type at compile time. The size is the amount of RAM used by the computer and is determined by the sizeof() operation.

Syntax of the Sizeof() Operator in C++

The following describes the syntax of the sizeof() operator:

# sizeof(name of data_type);

When used with the first type of object, the sizeof() function outputs the object’s size in bytes for the type of object that is being sent. The size of the item that is in the type of expression, measured in bytes, is the second type. After the expression is assessed, the size remains the same.

Example 1:

When the data types like int, float, char, etc. are used with the sizeof() operator, the function merely gives the amount of RAM that is allotted to that particular data type.

#include <iostream>

using namespace std;

int main()

{  
  std::cout << "Size of data type int : " <<sizeof(int)<< std::endl;  
  std::cout << "Size of data type float : " <<sizeof(float)<< std::endl;  
  std::cout << "Size of data type  double: " <<sizeof(double)<< std::endl;  
  std::cout << "Size of data type char: " <<sizeof(char)<< std::endl;  
  return 0;  
}

By utilizing the sizeof() operator with the cout command, we evaluate the size of the built-in data types in the aforementioned application. As we can see in the following output, the sizeof() operation yields the same answer as we would expect given our knowledge of the byte sizes of int, float, double, and char.

Example 2:

The program, when a class type is operand, uses the sizeof() operator. Here is an illustration:

#include <iostream>

using namespace std;

class MyBase_Class

{

int x;

};

int main()

{

MyBase_Class y;

std::cout << "Size of Base class is : "<<sizeof(y) << std::endl;

return 0;

}

With only one integer variable “x” in the class “MyBase_Class” and the aforementioned script main method, we evaluate the class’s size. First, we call the given class and create its object as “y”. Then, we have a sizeof() operator that takes “y” as an input. The integer variables take up 4 bytes, so the outcome would be 4 bytes as printed in the following:

Example 3:

If we increase the number of integer variables in a class, the following code is produced:

#include <iostream>

using namespace std;

class MyClass_Base  
{  
    int i;  
    int j;  
};  
int main()  
{  
  MyClass_Base j;  
  std::cout << "Size of MyClass_Base is : "<<sizeof(j) << std::endl;  
  return 0;  
}

There is now a second integer variable “j” in the previous code. Given that the int variable takes up 4 bytes and there are two integer variables as “i” and “j”, the “MyClass_Base” class in this scenario is 8 bytes in size.

Example 4:

The code might resemble the following if we include a char variable:

#include <iostream>

using namespace std;

class MyClass

{  
    int p;  
    int q;  
    char ch;  
};  
int main()  
{  
  MyClass q;  
  std::cout << "Size of MyClass is : "<<sizeof(q) << std::endl;  
    return 0;  
}

Two integer and one char variable are used in the “MyClass” class’s previous code. The size of the class should be 9 bytes as there are three data types (int+int+char) according to our calculations. But this is incorrect because of the idea of structure padding. You can see the following result value:

Example 5:

The case where the operand is an array. Let’s take a look at the program implementation:

#include <iostream>

using namespace std;

int main()

{  
  int arr_set[]={5,15,25,35,45};  
  std::cout << "Size of the 'arr_set' is : "<<sizeof(arr_set) << std::endl;  
  return 0;  
}

We established an integer type array as “arr_set” with five elements in the previous program. We determined the array’s size using the sizeof() operator. The array is 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 following output, the sizeof() operation produced the same result.

Example 6:

The example already takes the size of the array. Let’s consider an array from a particular perspective.

#include <iostream>

using namespace std;

void new_function(int array[])

{  
    std::cout << "Size of array is : " <<sizeof(array)<< std::endl;  
}  
int main()  
{  
  int array[]={51,52,53,54,55};  
  new_function(array);  
  return 0;  
}

We tried utilizing the “new_function” function in the aforementioned application to print the array’s size. We formed an integer-type array in this instance, and we send the “array” parameter to the new_function(). The size of the integer pointer or int* is returned by the new_function(). In the 64-bit operating systems, the size of the int* is 8 bytes. The output generates the array size as follows:

Example 7:

Now, this is the example where the argument is of the type of a pointer inside the sizeof() operator.

#include <iostream>

using namespace std;

int main()

{  
    int *ptr1= new int(31);  
    std::cout << "ptr1 size:" <<sizeof(ptr1)<< std::endl;
    std::cout << "*ptr1 size :" <<sizeof(*ptr1)<< std::endl;  
   double *ptr2= new double(20.99);
   std::cout <<"ptr2 size :" <<sizeof(ptr2)<< std::endl;  
   std::cout <<" *ptr2 size :"<<sizeof(*ptr2)<< std::endl;    
   char *ptr3=new char('m');  
    std::cout <<"ptr3 size :" <<sizeof(ptr3)<< std::endl;
    std::cout <<"*ptr3 size :"<<sizeof(*ptr3)<< std::endl;  
    return 0;  
}

We calculated the size of pointers in the script mentioned in the previously. For the data types “int”, “double”, and “char”, the pointer sizes remain constant. The size of the pointer is 4 bytes if the computer had a 32-bit operating system. The size of the pointer is 8 bytes if the computer runs a 64-bit operating system. The result is 8 bytes as we execute this script on a 64-bit platform. Now, if we give the pointer the symbol “*,” the output changes depending on the data type. For instance, if *ptr1 is an integer type, the sizeof() operator gives 4 bytes because an int data type takes up 4 bytes. The following is the resultant size of pointers corresponding to each specified data type:

Example 8:

The example where we have an expression as a sizeof() operator operand.

#include <iostream>

using namespace std;

 

int main()

{  
   int integer_1;  
   double integer_2;  
   cout << "size of int and double: " <<sizeof(integer_1+integer_2);  
     return 0;  
}

We have two variables, integer_1 and integer_2, of the int and double types, respectively. They are declared in the program. As we know, integers are 4 bytes in size while doubles are 8 bytes. We added both of them in the sizeof() operator. The variable, double with an 8-byte size, is the outcome.

Conclusion

A sizeof() explanation for C++ ends here. We have gone over the specifics on how the C++ sizeof() operator operates, along with relevant examples. Any data type, expression, array, etc. can have its size determined using the sizeof() operator. The data type or an expression must be provided as a part of the argument. And the function returns the size of that data type in bytes as the result.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.