DEV Community

Cover image for Understanding the sizeof Operator in C++: A Comprehensive Guide
Kevin Bjorvand
Kevin Bjorvand

Posted on

Understanding the sizeof Operator in C++: A Comprehensive Guide

Wassup guys!

I recently published a YouTube tutorial on the sizeof operator in C++, and I wanted to share some insights and gems from the video here. You can watch the full tutorial here

What is the sizeof Operator?
The sizeof operator in C++ is a compile-time operator that returns the size of a variable or data type in bytes. This can be incredibly useful for understanding how much memory your variables and structures are consuming.

Key Points Covered in the Tutorial:
Basic Usage:

`int a;
std::cout << "Size of int: " << sizeof(a) << " bytes" << std::endl;
This prints the size of an integer variable.`
Enter fullscreen mode Exit fullscreen mode

Using sizeof with Data Types:

`std::cout << "Size of double: " << sizeof(double) << " bytes" << std::endl;
You can directly use sizeof with data types to get their size.`
Enter fullscreen mode Exit fullscreen mode

Arrays and sizeof:

`int arr[10];
std::cout << "Size of array: " << sizeof(arr) << " bytes" << std::endl;
This returns the total size of the array (number of elements multiplied by the size of each element).`
Enter fullscreen mode Exit fullscreen mode

Pointers vs. Arrays:

`int* ptr;
std::cout << "Size of pointer: " << sizeof(ptr) << " bytes" << std::endl;
It's crucial to understand the difference in size between pointers and the arrays they may point to.`
Enter fullscreen mode Exit fullscreen mode

Best Practices:
Using sizeof with Structs and Classes:

`struct MyStruct {
    int a;
    double b;
    char c;
};
std::cout << "Size of MyStruct: " << sizeof(MyStruct) << " bytes" << std::endl;`
Enter fullscreen mode Exit fullscreen mode

This helps you understand the memory layout of your custom data types, which is critical for performance tuning and debugging.

Alignment and Padding:
The sizeof operator reveals the effects of alignment and padding on the size of structs and classes. Understanding this can help optimize memory usage.

`struct MyPackedStruct {
    char a;
    int b;
} __attribute__((packed));
std::cout << "Size of MyPackedStruct: " << sizeof(MyPackedStruct) << " bytes" << std::endl;`
Enter fullscreen mode Exit fullscreen mode

Using sizeof with Dynamic Memory:
Be cautious when using sizeof with dynamically allocated memory, as it only returns the size of the pointer, not the allocated memory.

`int* dynamicArray = new int[10];
std::cout << "Size of dynamicArray pointer: " << sizeof(dynamicArray) << " bytes" << std::endl;`
Enter fullscreen mode Exit fullscreen mode

Common Pitfalls:
Misunderstanding sizeof with Functions:

`void func(int arr[10]) {
    std::cout << "Size of arr inside function: " << sizeof(arr) << " bytes" << std::endl;
}`
Enter fullscreen mode Exit fullscreen mode

Inside functions, arrays decay to pointers, so sizeof will not return the size of the original array.

Using sizeof with Expressions:

std::cout << "Size of expression: " << sizeof(1 + 2.0) << " bytes" << std::endl;
Enter fullscreen mode Exit fullscreen mode

This will return the size of the resulting type of the expression.

Conclusion
Understanding the sizeof operator is fundamental for mastering C++. It not only helps in efficient memory management but also deepens your understanding of how C++ manages data. For a detailed explanation and more examples, check out my YouTube tutorial.

Feel free to leave comments and questions below. Happy coding!

Check out my full video here: https://www.youtube.com/watch?v=DuiCjk2ksfc&t=4s

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas • Edited

To use packed, you have to know what you're doing. There's no guarantee that a particular CPU can access unaligned data without causing a bus error. The better and cross-platform way to optimize struct storage would be to sort by member size, descending.

That aside, you never give any examples of using sizeof for reasons other than merely inquiring what the size is so you can print it out. (No, I'm not going to watch your video. For technical topics, text is far superior.)

Collapse
 
kevinbjorv profile image
Kevin Bjorvand

This post was supposed to be a sneak peek of what the video was about. I used a lot of time to explain alignment and memory for beginners in the video 😄