Home C sizeof operator in C++ tutorial

sizeof operator in C++ tutorial

The sizeof operator in C++ is used to determine the size, in bytes, of a data type or variable.

It’s evaluated at compile-time and is extremely useful for memory management, especially in situations involving arrays, structures, and dynamic memory allocation.

Syntax of sizeof

sizeof(data_type)
sizeof(variable)
  • data_type: The type whose size you want to know, e.g., int, float, char, etc.
  • variable: The variable whose size you want to know.

1. Using sizeof with Basic Data Types

The sizeof operator can be used with basic data types like int, float, double, char, etc.

#include <iostream>
using namespace std;

int main() {
    cout << "Size of int: " << sizeof(int) << " bytes" << endl;
    cout << "Size of float: " << sizeof(float) << " bytes" << endl;
    cout << "Size of double: " << sizeof(double) << " bytes" << endl;
    cout << "Size of char: " << sizeof(char) << " bytes" << endl;

    return 0;
}

Explanation:

  • Each sizeof expression returns the number of bytes occupied by each data type on the system.

Output (may vary depending on system):

Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 bytes

2. Using sizeof with Variables

The sizeof operator can be applied to variables to find out how much memory they occupy.

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    float b = 3.14f;
    char c = 'A';

    cout << "Size of variable a: " << sizeof(a) << " bytes" << endl;
    cout << "Size of variable b: " << sizeof(b) << " bytes" << endl;
    cout << "Size of variable c: " << sizeof(c) << " bytes" << endl;

    return 0;
}

Explanation:

  • sizeof(a), sizeof(b), and sizeof(c) return the size of each variable based on its data type.

Output:

Size of variable a: 4 bytes
Size of variable b: 4 bytes
Size of variable c: 1 bytes

3. Using sizeof with Arrays

sizeof is especially useful with arrays, as it helps determine the total size of the array in memory.

#include <iostream>
using namespace std;

int main() {
    int arr[5] = {1, 2, 3, 4, 5};

    cout << "Size of array arr: " << sizeof(arr) << " bytes" << endl;
    cout << "Size of each element in arr: " << sizeof(arr[0]) << " bytes" << endl;
    cout << "Number of elements in arr: " << sizeof(arr) / sizeof(arr[0]) << endl;

    return 0;
}

Explanation:

  • sizeof(arr) gives the total size of the array.
  • sizeof(arr[0]) gives the size of a single element.
  • Dividing sizeof(arr) by sizeof(arr[0]) calculates the number of elements in the array.

Output:

Size of array arr: 20 bytes
Size of each element in arr: 4 bytes
Number of elements in arr: 5

4. Using sizeof with Pointers

The size of a pointer depends on the architecture of the system (typically 4 bytes on 32-bit systems and 8 bytes on 64-bit systems), regardless of the data type it points to.

#include <iostream>
using namespace std;

int main() {
    int *intPtr;
    double *doublePtr;
    char *charPtr;

    cout << "Size of int pointer: " << sizeof(intPtr) << " bytes" << endl;
    cout << "Size of double pointer: " << sizeof(doublePtr) << " bytes" << endl;
    cout << "Size of char pointer: " << sizeof(charPtr) << " bytes" << endl;

    return 0;
}

Explanation:

  • sizeof(intPtr), sizeof(doublePtr), and sizeof(charPtr) all return the same size, as pointers are of a fixed size on a given system.

Output (may vary depending on system):

Size of int pointer: 8 bytes
Size of double pointer: 8 bytes
Size of char pointer: 8 bytes

5. Using sizeof with Structures

The sizeof operator can also be used to determine the size of a struct.

The size of a structure depends on the data types of its members and any alignment or padding added by the compiler.

#include <iostream>
using namespace std;

struct Person {
    char gender;
    int age;
    double height;
};

int main() {
    cout << "Size of struct Person: " << sizeof(Person) << " bytes" << endl;

    return 0;
}

Explanation:

  • sizeof(Person) returns the size of the entire structure, which includes any padding that the compiler may add for alignment.

Output (may vary depending on padding and alignment):

Size of struct Person: 16 bytes

6. Using sizeof with Classes

Classes in C++ also occupy memory, which includes memory for member variables and padding for alignment.

#include <iostream>
using namespace std;

class Rectangle {
public:
    int length;
    int width;
};

int main() {
    cout << "Size of class Rectangle: " << sizeof(Rectangle) << " bytes" << endl;

    return 0;
}

Explanation:

  • sizeof(Rectangle) returns the size of the class, which includes only the member variables (length and width), as member functions do not occupy memory per instance.

Output:

Size of class Rectangle: 8 bytes

7. Using sizeof with Dynamic Memory Allocation

The sizeof operator can also be used to determine the size of dynamically allocated memory, making it helpful for memory management tasks.

#include <iostream>
using namespace std;

int main() {
    int *ptr = new int[5];
    cout << "Size of pointer ptr: " << sizeof(ptr) << " bytes" << endl;
    cout << "Size of allocated array (not directly possible): Use known element size and count." << endl;

    delete[] ptr; // Free the allocated memory
    return 0;
}

Explanation:

  • sizeof(ptr) returns the size of the pointer itself, not the dynamically allocated memory.
  • The size of the allocated array can be calculated if the element size and count are known.

Output:

Size of pointer ptr: 8 bytes
Size of allocated array (not directly possible): Use known element size and count.

8. Using sizeof with Character Strings

sizeof can be used with character arrays, but keep in mind that it will include the null terminator (\0).

#include <iostream>
using namespace std;

int main() {
    char str[] = "Hello";
    
    cout << "Size of string str: " << sizeof(str) << " bytes" << endl;
    cout << "Length of string (excluding null): " << sizeof(str) - 1 << " bytes" << endl;

    return 0;
}

Explanation:

  • sizeof(str) returns the total size of the character array, including the null terminator.
  • Subtracting 1 from sizeof(str) gives the length of the string without the null character.

Output:

Size of string str: 6 bytes
Length of string (excluding null): 5 bytes

9. Using sizeof with Enum Types

The sizeof operator can be used to find the size of an enumeration.

Typically, enums are stored as integers.

#include <iostream>
using namespace std;

enum Days { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY };

int main() {
    cout << "Size of enum Days: " << sizeof(Days) << " bytes" << endl;

    return 0;
}

Explanation:

  • sizeof(Days) returns the size of the enum, which is generally the size of an int (4 bytes on most systems).

Output:

Size of enum Days: 4 bytes

10. Summary Table of sizeof Use Cases in C++

Use Case Example Description
Basic data types sizeof(int) Finds size of fundamental data types
Variables sizeof(x) Finds size of a variable
Arrays sizeof(arr) Finds total size of array in bytes
Array elements sizeof(arr[0]) Finds size of one element in an array
Pointers sizeof(ptr) Finds size of a pointer
Structures sizeof(Person) Finds size of a struct, including padding
Classes sizeof(Rectangle) Finds size of a class, including members
Dynamic memory sizeof(ptr) Finds size of a dynamically allocated pointer
Character strings sizeof(str) Includes null terminator for character arrays
Enums sizeof(Days) Finds size of an enum, typically an int

Complete Example

This example demonstrates using sizeof with different data types, variables, structures, and arrays.

#include <iostream>
using namespace std;

struct Employee {
    int id;
    double salary;
};

int main() {
    int x = 10;
    float y = 5.5f;
    char ch = 'A';
    int arr[5] = {1, 2, 3, 4, 5};
    Employee emp;

    cout << "Size of int: " << sizeof(int) << " bytes" << endl;
    cout << "Size of float: " << sizeof(float) << " bytes" << endl;
    cout << "Size of char: " << sizeof(char) << " bytes" << endl;
    cout << "Size of variable x: " << sizeof(x) << " bytes" << endl;
    cout << "Size of array arr: " << sizeof(arr) << " bytes" << endl;
    cout << "Size of struct Employee: " << sizeof(Employee) << " bytes" << endl;
    cout << "Size of pointer to int: " << sizeof(int*) << " bytes" << endl;

    return 0;
}

Sample Output:

Size of int: 4 bytes
Size of float: 4 bytes
Size of char: 1 bytes
Size of variable x: 4 bytes
Size of array arr: 20 bytes
Size of struct Employee: 16 bytes
Size of pointer to int: 8 bytes

The sizeof operator in C++ provides essential information about memory usage, helping to make decisions for memory management and data structure layout.

You may also like