Selected Reading

C++ sizeof Operator



The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type.

The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.

Syntax

sizeof (data type)

Where data type is the desired data type including classes, structures, unions, and any other user-defined data type.

sizeof with Primitive Data Types

The sizeof the operator in C++ can be used to determine the size (in bytes) of primitive data types like int, float, double, char, etc. The size of these depends on the system architecture, but common sizes are int (4 bytes), float (4 bytes), double (8 bytes), and char (1 byte) etc.

Lets go through all with an Examples

Example

Heres an example covering all primitive data types with their sizes as an output.

#include <iostream>

int main() {
   std::cout << "Size of char: " << sizeof(char) << " bytes\n";
   std::cout << "Size of short: " << sizeof(short) << " bytes\n";
   std::cout << "Size of int: " << sizeof(int) << " bytes\n";
   std::cout << "Size of long: " << sizeof(long) << " bytes\n";
   std::cout << "Size of float: " << sizeof(float) << " bytes\n";
   std::cout << "Size of double: " << sizeof(double) << " bytes\n";
   std::cout << "Size of long double: " << sizeof(long double) << " bytes\n";
   std::cout << "Size of bool: " << sizeof(bool) << " bytes\n";

   std::cout << "Size of void: " << sizeof(void) << " bytes\n"; 
   // This is not valid and will result in a compile-time error.

   return 0;
}

Output

Size of char: 1 bytes
Size of short: 2 bytes
Size of int: 4 bytes
Size of long: 8 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of long double: 16 bytes
Size of bool: 1 bytes
Size of void: 1 bytes

sizeof with Arrays

The sizeof working with arrays depends on whether the array is statically declared (fixed-size) or dynamically allocated (via malloc, new, etc.).

Here we will cover and see the behavior of both arrays in each case.

A statically declared array is an array whose size is fixed at compile time. It is stored in stack memory, where sizeof can give the total size and the number of elements directly.

A dynamically allocated array is created during runtime using malloc(), calloc(), new, or similar memory allocation functions. Here, the array is allocated on the heap, where sizeof only gives the size of the pointer, so you must manually track the number of elements.

Example

#include <iostream>

int main() {

   // Static array
   int staticArr[4] = {1, 2, 3, 4};

   std::cout << "Static array size: " << sizeof(staticArr) << " bytes\n";
   std::cout << "Static array elements: " << sizeof(staticArr)
      / sizeof(staticArr[0]) << "\n";

   // Dynamic array
   int* dynamicArr = new int[4];

   std::cout << "Dynamic array pointer size: " << sizeof(dynamicArr) << " bytes\n";

   // Clean up
   delete[] dynamicArr;

   return 0;
}

Output

Static array size: 16 bytes
Static array elements: 4
Dynamic array pointer size: 8 bytes

sizeof with Pointers

The sizeof the operator can be used to determine the size of pointers in C++, which represents the amount of memory used to store the pointer itself, not the data it points to.

Example

#include <iostream>

int main() {
   int a = 5;
   double b = 3.14;
   char c = 'A';

   std::cout << "Size of pointer to int: " << sizeof(&a) << " bytes\n"
   << "Size of pointer to double: " << sizeof(&b) << " bytes\n"
   << "Size of pointer to char: " << sizeof(&c) << " bytes\n";

   int d = 5;
   int* ptr = &d;

   std::cout << "Size of the data pointed by ptr: " << sizeof(*ptr) << 
      " bytes" << std::endl;

   return 0;
}

Output

Size of pointer to int: 8 bytes
Size of pointer to double: 8 bytes
Size of pointer to char: 8 bytes
Size of the data pointed by ptr: 4 bytes

sizeof with Structures, Classes, and Unions

The sizeof operator helps to determine the memory size of structures, classes, and unions.

  • A structure (struct) groups variables of different types, and its size includes the total size of its members.
  • A structure (struct) groups variables of different types, and its size includes the total size of its members.
  • A union allows different types to share the same memory space, and its size is determined by the largest member since all members overlap in memory.

Example

#include <iostream>

// Structure Example
struct MyStruct {
   char c;      // 1 byte
   int i;       // 4 bytes
   double d;    // 8 bytes
};

// Class Example
class MyBase {
public:
   virtual void foo() {} 
};

class MyClass : public MyBase {
public:
   char c;      // 1 byte
   int i;       // 4 bytes
   double d;    // 8 bytes
   virtual void bar() {} 
};

// Union Example
union MyUnion {
   char c;      // 1 byte
   int i;       // 4 bytes
   double d;    // 8 bytes
};

int main() {
   std::cout << "Size of MyStruct: " << sizeof(MyStruct) << " bytes\n";
   std::cout << "Size of MyClass: " << sizeof(MyClass) << " bytes\n";
   std::cout << "Size of MyUnion: " << sizeof(MyUnion) << " bytes\n";

   return 0;
}

Output

Size of MyStruct: 16 bytes
Size of MyClass: 24 bytes
Size of MyUnion: 8 bytes

Limitation of sizeof

  • sizeof does not account for dynamically allocated memory or the memory managed by containers like std::string and std::vector.
  • sizeof cannot determine the size of memory for base class objects in derived classes with inheritance.
  • sizeof returns the size of a pointer when applied to dynamically allocated memory, not the size of the allocated block.
  • The result of sizeof does not include padding added for alignment purposes or the size of the virtual table (vtable) for classes with virtual functions.
  • sizeof cannot be used with incomplete types (forward-declared classes or types).
Advertisements