×

Sizeof() Operators in C++

sizeof() Operators in C++

The sizeof() operator in C++ defines the size of variables, constants, or data types. It is a unique operator that manipulates other operators and returns the size of the variables or constants in the compile time.

Syntax:    

sizeof()(data_type)

Here, the data_type may be a constant, a variable, or any other structures or union.

Lets us see the coding illustration to visualize how sizeof() operator works.

Example 1:

 #include<bits/stdc++.h>
 using namespace std; 
 int main() 
 {
  cout << "Size of Integer : " <<sizeof(int)<<endl; 
  cout << "Size of float : " <<sizeof(float)<<endl; 
  cout << "Size of double : " <<sizeof(double)<<endl; 
  cout << "Size of char : " <<sizeof(char)<<endl; 
   return 0; 
 }  

Output:

sizeof() Operators in CPP

Explanation:

As already discussed, the sizeof() operator returns the size of any data type, constants, or any variable. In the above program, we have implemented the sizeof() operator to visualize how the sizeof() operator works in close integration with different data types.

Example 2:

 #include<bits/stdc++.h>
 using namespace std; 
 class Main
     { 
     int x; 
     }; 
 int main() 
 { 
     Main y; 
         cout << "Size of Main class is : "<<sizeof(y) <<endl; 
     return 0; 
 }  

Output:

sizeof() Operators in CPP

Explanation:

In the above, we have implemented the use of the sizeof() operator using class. We have defined a class 'Main,' with a data member 'x' of integer type. We have defined another integer variable in the driver code, 'b,' associate with class Main. Since we know that member 'a' is an integer, so in the driver code, it should print as 4 if we use the sizeof() operator. Therefore, 4 is printed on the console as the size of an integer variable in the class 'Main.'

Similarly, we can add more data members be it a char or a float. The sizeof() operator will return the total size of the variables or the data members assigned to some classes.

Lets us look how the sizeof() operator works for arrays.

Example 3:

 #include <bits/stdc++.h>
 using namespace std; 
 int main() 
 { 
   int array[]={101,202,303,404,505,606}; 
   cout << "Size of the given array is : "<< sizeof(array)<<endl; 
   return 0; 
 } 

Output:

sizeof() Operators in CPP

Explanation:

In the above code, we have used the sizeof() operator to calculate the total size of the linear data type i.e., array. The array in the above code is initialized with the 6 different values. As we know that an integer is of 4 bytes, so the sizeof() operator returns the value of the integer on the console as 24.

Example 4:

 #include <bits/stdc++.h>
 using namespace std; 
 int main() 
 { 
     int *pointer1=new int(11); 
         cout << "size of pointer 1 : " <<sizeof(pointer1)<<endl;                                                                                                                                                                                                                                  
         cout << "size of *pointer 1 : " <<sizeof(*pointer1)<<endl; 
    char *pointer2=new char('b'); 
         cout <<"size of pointer 2 : " <<sizeof(pointer2)<<endl; 
         cout <<"size of *pointer 2 : "<<sizeof(*pointer2)<<endl; 
    double *pointer3=new double(13.70); 
         cout <<"size of pointer 3 : " <<sizeof(pointer3)<<endl; 
         cout <<"size of *pointer 3 : "<<sizeof(*pointer3)<<endl; 
     return 0; 
 }  

Output:

sizeof() Operators in CPP

Explanation:

In the above code, we have used the sizeof() operator to illustrate its working with the pointers. Therefore, we have constructed three-pointers, namely 'pointer1', 'pointer2', 'pointer3' associated with three different data types int, char, and double(float). We have assigned values for these data types, respectively. Since we are very familiar with the size of different data types, the program returns their size concerning their original value and their associated pointer. The sizes are then displayed on the console as outputs.

Advantages of using sizeof() operator

The advantages are listed below:

  1. Acts as a savior

Whenever we want to know the number of elements in an array or the type of fashion in which they are arranged, the sizeof() operator acts as a savior as it automatically does that.

2. Helps in Memory allocation

It adds a great advantage in dynamic programming as it allows the programmer to dynamically allocate the size of the block of memory. It helps in memory allocation with enough prior knowledge to allocate enough memory. The memory is allocated in a particular machine which is difficult to do with other operators.

3. Versatile and Flexible

It is a versatile and flexible operator in C++, which helps in streamlining for the allocation of the memory.

Points to Remember:

  1. sizeof() operator is a compile-time unary operator that can be used to get the size of the operand.
  2. The result of the size of the result returned by the sizeof() operator is an unsigned integer value which depends on the nature of the data-type.
  3. Normally, the size of an object is the sum of its constituent data members. It is guaranteed that the sizeof() operator will never be smaller than that provided compiler keeps padding between data members to ensure alignment requirements of the platform.

Related Topics

DES in C++

The popularity of the Data Encryption Standard (DES) is slightly declining as a result of the discovery that DES is susceptible to very strong attacks. Since DES is a block cypher,...

3 minutes read.

Remove duplicates from sorted array in C++

Remove duplicates from the sorted array in C++. This same process, due to a sorted array, is to erase the redundant components from the array. Examples: Input  : arr[] = {2, 2, 2,...

4 minutes read.

Multiple Inheritance

C++ Multiple Inheritance Multiple inheritance is such an inheritance in which a derived class inherits properties of more than one base class.     C++ Multiple Inheritance Example In this example, two base classes Square and...

2 minutes read.

C++ Range-based For Loop

In C++ language, the range-based for loop was added, which is far superior than the ordinary For loop. The implementation of a range-based for loop doesn't really need much code. It's a...

4 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.

C++ Program to find the element that occurs once

Write a program to find the element in the array that occurs once. Given that all the numbers in the array are present two times and the array is sorted,...

7 minutes read.

Method overriding in C++

What is method overriding? Using the same function in derived class as their base class is referred to as function/method overriding in c++. Method overriding is an example of polymorphism. With...

2 minutes read.

INT_MAX and INT_MIN in C/C++

In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact...

3 minutes read.

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide...

2 minutes read.

C++ Global Variable

In C++, a global variable is a variable that is defined outside of any function or class and can be accessed by any function or class in the program. Global...

4 minutes read.

Multilevel Inheritance

C++ Multilevel Inheritance Multilevel inheritance is such an inheritance in which a derived class is created from another derived class. C++ Multilevel Inheritance Example In this example, a base class Student is inherited in...

2 minutes read.

10 Best C and C++ Books for Beginners & Advanced Programmers

If you want to become a skilled software developer, you should never stop learning, whether you're a working professional or a student. Why, therefore, only C or C++? The fundamental...

6 minutes read.

C++ Object Class

C++ Object Class Overview: C++ is a high-level programming language and an object-oriented programming language. An object-oriented language always has some properties of classes and objects. In this article, we...

4 minutes read.

Assertions in C/C++

Assertions are the statements used to check presumptions which is made by the programmers. Example: Assertion is used to verify whether the malloc returned by the pointer is NULL or not. For...

4 minutes read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

Sum of all elements between k1’th and k2’th Smallest Elements

In this tutorial, we will look at how to determine sum of all given elements between two given indexes’ smallest elements. Assuming an array of integers and two numbers, k1...

2 minutes read.

C++ Expressions

C ++ equations are made up of operators, constants and variables arranged according to language rules. It may also include function calls that give results. To calculate the value, the...

4 minutes read.

Naming Convention in C++

The first and most fundamental step a programmer takes to produce clean code is to name a file or a variable. This naming must be acceptable so that it serves...

5 minutes read.