โž• C++ Operators
Estimated reading: 3 minutes 281 views

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 sizeof operator
  • 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 sizeof on a pointer expecting array sizeOnly gives pointer size (4 or 8 bytes)
Forgetting parentheses with typessizeof int โ†’ sizeof(int)
Using sizeof() in runtime evaluationWorks only at compile-time

Best Practices

  • Use sizeof(var) instead of sizeof(type) when working with templates or generic functions
  • Combine sizeof with strlen() carefully โ€” sizeof counts memory, not content length
  • Prefer sizeof(var) for readability and type safety

Summary โ€“ Recap & Next Steps

Key Takeaways:

  • sizeof tells you how many bytes a data type or variable occupies
  • Works at compile time, returning a size_t value
  • 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 :
Share

C++ sizeof Operator

Or Copy Link

CONTENTS
Scroll to Top