๐ C++ sizeof Operator โ Measure Memory Usage in Bytes
๐งฒ Introduction โ What Is the sizeof Operator in C++?
The sizeof operator in C++ is used to determine the size (in bytes) of data types or variables during compile time. It helps developers understand how much memory different data types consume and is essential for memory-efficient programming, especially in low-level or system applications.
๐ฏ In this guide, youโll learn:
- Syntax and usage of the sizeofoperator
- How it works with primitive and complex data types
- Usage in arrays, pointers, and structures
- Common pitfalls and type safety tips
โ
 Syntax of sizeof
sizeof(type)         // e.g., sizeof(int)
sizeof(expression)   // e.g., sizeof(myVar)
Both return a size_t value (an unsigned integer type).
๐งฎ Example โ Using sizeof with Basic Types
#include <iostream>
using namespace std;
int main() {
    cout << "Size of int: " << sizeof(int) << " bytes\n";
    cout << "Size of float: " << sizeof(float) << " bytes\n";
    cout << "Size of char: " << sizeof(char) << " bytes\n";
    return 0;
}
๐ Typical output on 64-bit systems:
Size of int: 4 bytes  
Size of float: 4 bytes  
Size of char: 1 byte  
๐ฆ sizeof with Variables
int x = 42;
cout << sizeof(x);  // Same as sizeof(int)
โ Useful when working with generic types or templates.
๐งฎ sizeof with Arrays
int arr[10];
cout << sizeof(arr);           // 40 bytes (if int = 4 bytes)
cout << sizeof(arr) / sizeof(arr[0]);  // Array length = 10
โ ๏ธ sizeof only works for arrays at compile-time (not with pointers).
๐๏ธ sizeof with Structures and Classes
struct Point {
    int x;
    double y;
};
cout << sizeof(Point);  // Includes padding for alignment
๐ Structure sizes may vary due to padding and alignment.
โ ๏ธ Common Mistakes
| โ Mistake | โ Fix | 
|---|---|
| Using sizeofon a pointer expecting array size | Only gives pointer size ( 4or8bytes) | 
| Forgetting parentheses with types | sizeof intโ โsizeof(int)โ | 
| Using sizeof()in runtime evaluation | Works only at compile-time | 
๐ง Best Practices
- Use sizeof(var)instead ofsizeof(type)when working with templates or generic functions
- Combine sizeofwithstrlen()carefully โsizeofcounts memory, not content length
- Prefer sizeof(var)for readability and type safety
๐ Summary โ Recap & Next Steps
๐ Key Takeaways:
- sizeoftells you how many bytes a data type or variable occupies
- Works at compile time, returning a size_tvalue
- Use it to calculate array sizes, structure memory, and optimize memory usage
- Be cautious with pointers and padding in structs
โ๏ธ Real-World Relevance:
The sizeof operator is vital in memory-sensitive applications, embedded development, data serialization, and file I/O for binary formats.
โ FAQs โ C++ sizeof Operator
โ Is sizeof evaluated at runtime?
โ No. It is evaluated at compile time.
โ What does sizeof(ptr) return?
โ
 Returns the size of the pointer itself (usually 4 or 8 bytes), not the size of what it points to.
โ How do I get the number of elements in an array?
โ
 Use:
sizeof(arr) / sizeof(arr[0])
โ Can I use sizeof with class or struct?
โ
 Yes. It returns the size of all members including padding.
โ Does sizeof(char) always return 1?
โ
 Yes, by definition, sizeof(char) is always 1.
Share Now :
