sizeof() operator works In Pointer C/C++

Share Your Love

The sizeof() operation determines the size of a data type, a constant, or a variable. It’s a compile-time operator since it calculates the size of any variable or constant at build time.

The sizeof() operation calculates the amount of RAM used by the computer.

How sizeof() the operator work?

sizeof(data_type);  

Let’s look at an example to better comprehend this situation.

#include <iostream>  
using namespace std;  
int main()  
{  
  // Determining the space in bytes occupied by each data type.  
  std::cout << "Size of integer data type : " <<sizeof(int)<< std::endl;  
  std::cout << "Size of float data type : " <<sizeof(float)<< std::endl;  
  std::cout << "Size of double data type : " <<sizeof(double)<< std::endl;  
  std::cout << "Size of char data type : " <<sizeof(char)<< std::endl;  
  return 0;  
} 

We used the sizeof() operator in the above program to determine the size of the built-in data types. As we know, an int takes up 4 bytes, a float takes up 4 bytes, a double takes up 8 bytes, and a char takes up 1 byte. The sizeof() operation produces the same result, as demonstrated in the following output.

Output:

Size of integer data type : 4
Size of float data type : 4
Size of double data type : 8
Size of char data type : 1

When an operand is of pointer type:

#include <iostream>  
using namespace std;  
int main()  
{  
    int *ptr1=new int(10);  
    std::cout << "size of ptr1 : " <<sizeof(ptr1)<< std::endl;                                                  
   std::cout << "size of *ptr1 : " <<sizeof(*ptr1)<< std::endl;  
   char *ptr2=new char('a');  
   std::cout <<"size of ptr2 : " <<sizeof(ptr2)<< std::endl;  
   std::cout <<"size of *ptr2 : "<<sizeof(*ptr2)<< std::endl;  
   double *ptr3=new double(12.78);  
    std::cout <<"size of ptr3 : " <<sizeof(ptr3)<< std::endl;  
   std::cout <<"size of *ptr3 : "<<sizeof(*ptr3)<< std::endl;  
    return 0;  
}  

The size of the pointers was determined in the preceding program. For all data types, the size of pointers would be the same.

If the computer runs on a 32-bit operating system, the pointer will be 4 bytes in size. If the computer runs on a 64-bit operating system, the pointer will be 8 bytes in size.

This application is executing in 64-bit mode, thus the output will be 8 bytes. If we give the pointer the ‘*’ symbol, the result depends on the data type; for example, if *ptr1 is of integer type, the sizeof() operation will return 4 bytes because the int data type takes up 4 bytes.

Output:

size of ptr1 :  8
size of *ptr1 :  4
size of ptr2 :  8
size of *ptr2 :  1
size of ptr3 :  8
size of *ptr3 :  8

If you like please share and improve this post connect us.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 355

Newsletter Updates

Enter your email address below to subscribe to our newsletter