Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Sizeof() Operator in C++

Size of operator in C++

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

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

Example:

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

O/P:

size of long double in devc:16 bytes
Sizeof() operator in C++

C++ program demonstrating size of data types and variables

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);//nothing but size of int
cout<<"\nsize of b:"<<sizeof(b);//nothing but size of float
}

O/P

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 a function

We can display the function size directly with its name:
ex1:

char main()
{
cout<<"size of main:"<<sizeof(main())<<"bytes";//size of char(return_type)
}

O/P

size of main:1bytes

ex2:

void main()
{
cout<<"size of main:"<<sizeof(main())<<"bytes";//0 bytes for void
}

o/p

size of main:0bytes

Size of Array

It is nothing but the product of the size of array type with the number of elements

Example:

int a[10];//10*sizeof(int) =10*4 =40 bytes
main()
{
int a[5]={1,2,3,4,5};//interger array 5*4 bytes
char b[]={'t','r','i','s','h','a','a','n','k'};//unsized charecter array 9*1;

cout<<"\nsize of int array:"<<sizeof(a)<<"bytes";
cout<<"\nsize of char array:"<<sizeof(b)<<"bytes";
}

O/P

size of int array 20bytes
size of char array 9 bytes

Size of structure 

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

struct student// student as structure name
{
int sid;//structure members
char sname[20];
float marks, attendance;
}trish;
main()
{
cout<<"size of trish:"<<sizeof(trish);//32 bytes
}

O/P

size of trish:32bytes

Size of structure variable in above

  • int (4bytes)+char(20*1 =20 bytes)+float(2*4=8bytes)=32 bytes
  • Hence trish 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

union demo//declaring union
{
int a;
float b;
}d;// 4 bytes allocated
main()
{
cout<<"\nmemory allocated for this union is "<<sizeof(d)<<"bytes";
}

O/P

a:12 b:12.000
a:14 memory allocated for this union is 4 bytes

o/p

memory allocated for this union is 4 bytes

In the above example size of the union is 4 bytes because among int a and float b, float is larger than int hence .hence size of the float will be the size of the union here

Size of constants

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

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

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 i.e 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
class test
{
public:
int a =2;
float b=3;
};
main()
{
cout<<"\nsize of test:"<<sizeof(test);//sum of int+flaot =4+4= 8 bytes
test t;
cout<<"\nsize of test:"<<sizeof(t);//same as above
}

Size of operator in an expression

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

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

O/P

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

main()
{
int num1,num2;
cout << sizeof(num1*sizeof(num2));//sizeof iniside a sizeof
}

Output:

4