Sizeof() Operator in C++

Sizeof() Operator

On this page we will discuss sizeof() operator in C++. sizeof()is a compile-time operator used to determine the size of variables or any user-defined, predefined datatypes .So we will see sizeof() operator in detail on this page with some programs.
sizeof() operator in C++

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";

Output:

size of long double:
16 bytes

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

Run
#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:

Run

#include 
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

Run
#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 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

Run
#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

Run
#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:

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
Run
#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

Run
#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

Run
#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

8
8

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

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


  • vivek

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