Sizeof() Operator in C++

Sizeof() Operator

Here, in this section, we will discuss sizeof() operator in C++.

Definition: sizeof()is a compile-time operator used to determine the size of variables or any user-defined, predefined datatypes

size

More about sizeof() operator

  • Some times User is interested to know the amount of memory allocated by the compiler,
  • C++ designers provided with sizeof(data)keyword to serve this purpose
  • The output of sizeof() operator is integer format and in terms of bytes.

Example:

cout << "size of long double :"; 
cout << sizeof(long double) << " bytes";

O/P:

size of long double:
16 bytes

C++ program demonstrating the size of data types and variables

#include<iostream>
using namespace std;

int main() 
{ 
    //size with datatype names 
    cout << "Size of char : " << sizeof(char) << endl; 
    cout << "Size of int : " << sizeof(int) << endl; 
    cout << "Size of short int : " << sizeof(short int) << endl; 
    cout << "Size of double : " << sizeof(double) << endl; 
    cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; 
    
    //size with variable names 
    int a; 
    float b;
    
    cout << "\nSize of a: " << sizeof(a);
    cout << "\nSize of b: " << sizeof(b);

    return 0;
}

Output

Size of char : 1
Size of int : 4
Size of short int : 2
Size of double : 8
Size of wchar_t : 4

Size of a: 4
Size of b: 4

Size of Array

An array may occupy a lot of memory. We may want to know how much memory is it occupying.

We can do this with the help of the following code

Example:

#include<iostream>
using namespace std;

int main()
{
    // Note : int size - 4 bytes, char size - 1 bytes
    
    int arr1[6] = {1, 2, 3, 4, 5, 6}; // interger array 5 * 4 bytes
    char arr2[] = {'H', 'e', 'l', 'l', 'o'}; // unsized character array 5 * 1
    
    cout << "Size of int array: " << sizeof(arr1) << " bytes";
    cout << "\nSize of char array: " << sizeof(arr2) << " bytes";
    
    // we can even calculate number of elements in the array
    // (total array size) / (size of 1 array item)
    // 24 / 4 = 6
    int items = sizeof(arr1)/sizeof(arr1[0]);
    
    cout << "\n\nItems in arr1: " << items;

    return 0;
}

Output

Size of int array: 24 bytes
Size of char array: 5 bytes

Items in arr1: 6

Size of structure 

The size of the structure is the sum of sizes of individual members which is displayed  through structure variables

#include<iostream>
using namespace std;

// student as structure name
struct student
{   
    //structure members
    int sid;
    char sname[20];
    float marks, attendance;
}s1;

int main()
{
    cout << "Size of s1: " << sizeof(s1); // 32 bytes
    
    return 0;
}

Output

Size of s1: 32

Size of structure variable in above

  • int (4 bytes) + char(20 * 1 = 20 bytes) + float(2 * 4 = 8 bytes) = 32 bytes
  • Hence s1 is a variable of newly defined type student which can accommodate 32 bytes

Size of union

Memory allocated for the union is the size of the member which is having a larger size than all other members

#include<iostream>
using namespace std;

//declaring union
union demo
{
    int a;
    double b;
}d1;
// 8 bytes allocated

int main()
{
    cout << "Memory allocated: " << sizeof(d1) << " bytes";
    // union memory is allocated basis per the largest datatype in union
    // here the largest data type is double: which has size 8 bytes
    // so size of union demo would be 8 bytes

    return 0;
}

Output

Memory allocated: 8 bytes

Size of constants

For constants, the compiler performs implicit typecasting and by default determines the data type and  allocates memory

#include<iostream>
using namespace std;

int main()
{
    //int datatype size
    cout << "Size of integer constant: " << sizeof(10) << endl;
    //char datatype size
    cout << "Size of character constant : " << sizeof('a') << endl;
    //long datatype size
    cout << "Size of long constant : " << sizeof(9999999999999999999) << endl;

    return 0;
}

Output:

Size of integer constant: 4
Size of character constant : 1
Size of long constant: 8

Size of class and objects

  • Memory allocation mechanism in classes and objects is the same as structures
  • That is the size of the class is the sum of sizes of individual members
  • The size of the class or object both mean the same
  • The size of the class is displayed by the name of the class or the name of the object
#include<iostream>
using namespace std;

class test
{
    public:
    int a = 2;
    float b = 3.0;
    double c = 5.1;
};
//sum of sizes of int + float = 4 + 4 + 8 = 16 bytes
int main()
{
    // printing using name of class
    cout << "\nSize of test: " << sizeof(test);
    
    test t;
    // printing using name of object
    cout << "\nSize of test: " << sizeof(t); //same as above
    
    return 0;
}

Output

Size of test: 16
Size of test: 16

Size of operator in an expression

Size of operator can be used in expressions like ordinary values and variables

#include<iostream>
using namespace std;

int main()
{
    int a = 5;
    cout << a + sizeof(a) + sizeof(float); //5 + 4 + 4 = 13
    
    return 0;
}

Output

13

sizeof(a) is replaced with integer constant 4 and sizeof(float) is
replaced with integer constant 4, so 5 + 4 + 4 = 13

Nesting the sizeof() operator

size of() can be nested in any way like an ordinary expression

#include<iostream>
using namespace std;

int main()
{
    // Note return type of sizeof is size_t datatype which has size of 8 bytes
    cout << sizeof(size_t) << endl;

    int num1, num2;
    cout << sizeof(num1 * sizeof(num2)); //sizeof iniside a sizeof 
// num1 * sizeof(num2) => datatype(int) * datatype(size_t) // result of this would be a value of datatype(size_t) // sizeof(size_t) => 8 }

Output

4

One comment on “Sizeof() Operator in C++”


  • vivek

    cout << sizeof(num1*sizeof(num2));//sizeof iniside a sizeof
    please explain the above statement