Introduction to C++ | sizeof Operator

C++ sizeof Operator

The sizeof operator in C++ is used to determine the size, in bytes, of a data type or a variable. It is a compile-time operator that returns the size of the object or data type in memory. This operator is essential when working with dynamic memory allocation, arrays, and determining the memory requirements of your program.

What is the sizeof Operator?

The sizeof operator returns the size (in bytes) of a variable, data type, or object. This is useful when dealing with low-level memory manipulation or when you need to allocate memory dynamically and need to know the exact size of data types.

Syntax:

Syntax of sizeof Operator


        sizeof(dataType);
        sizeof(variable);
        sizeof(expression);
                    

Example of Using sizeof

Here’s an example where the sizeof operator is used to find the size of different data types:

Code Example: sizeof Operator


        #include <iostream>
        using namespace std;
        
        int main() {
            int num = 10;
            double pi = 3.14159;
            char letter = 'A';
        
            // Using sizeof operator
            cout << "Size of int: " << sizeof(int) << " bytes" << endl;
            cout << "Size of num: " << sizeof(num) << " bytes" << endl;
            cout << "Size of double: " << sizeof(pi) << " bytes" << endl;
            cout << "Size of char: " << sizeof(letter) << " bytes" << endl;
        
            return 0;
        }
                    

Output

Size of int: 4 bytes
Size of num: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte

sizeof with Arrays

The sizeof operator can also be used to find the size of an array. When applied to an array, sizeof returns the total size of the array in bytes. To determine the number of elements in the array, divide the total size of the array by the size of one element.

For example:

Code Example: sizeof with Arrays


        #include <iostream>
        using namespace std;
        
        int main() {
            int arr[] = {1, 2, 3, 4, 5};
        
            // Get the size of the array
            cout << "Size of the array: " << sizeof(arr) << " bytes" << endl;
        
            // Get the number of elements in the array
            cout << "Number of elements in the array: " << sizeof(arr) / sizeof(arr[0]) << endl;
        
            return 0;
        }
                    

Output

Size of the array: 20 bytes
Number of elements in the array: 5

sizeof with Pointers

When using the sizeof operator with pointers, it returns the size of the pointer itself, not the memory that the pointer points to. The size of a pointer is typically 4 or 8 bytes, depending on whether the system is 32-bit or 64-bit.

For example:

Code Example: sizeof with Pointers


        #include <iostream>
        using namespace std;
        
        int main() {
            int num = 10;
            int* ptr = #
        
            // Get the size of the pointer
            cout << "Size of pointer: " << sizeof(ptr) << " bytes" << endl;
        
            // Get the size of the variable the pointer points to
            cout << "Size of the variable the pointer points to: " << sizeof(*ptr) << " bytes" << endl;
        
            return 0;
        }
                    

Output

Size of pointer: 8 bytes (on a 64-bit system)
Size of the variable the pointer points to: 4 bytes

Conclusion

The sizeof operator is a powerful tool in C++ that allows you to determine the memory size of data types, variables, arrays, and pointers. This is useful for memory management, dynamic memory allocation, and working with low-level data structures. Remember that the size of a data type may vary depending on the platform and compiler being used.

Pro Tip:

💡 Pro Tip

The sizeof operator is evaluated at compile-time, meaning that it does not incur any runtime cost. However, keep in mind that when using it with expressions, such as in the case of arrays or pointers, it will return the size of the pointer or the array in memory, not the dynamically allocated memory size (if applicable).