- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Input Output Operations
- C++ Basic Input/Output
- C++ Cin
- C++ Cout
- C++ Manipulators
- Type System & Data Representation
- C++ Modifier Types
- C++ Storage Classes
- C++ Constexpr Specifier
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Scope Resolution Operator
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ Jump Statements
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Return Values
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Array Decay
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Destructors
- C++ Virtual Destructor
- C++ Inheritance
- C++ Inheritance
- C++ Multiple Inheritance
- C++ Multilevel Inheritance
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ Override Specifiers
- C++ Final Specifiers
- C++ Design Patterns
- C++ Creational Design Patterns
- C++ Singleton Design Pattern
- C++ Factory Method Design Pattern
- C++ Abstract Factory Pattern
- C++ Prototype Design Pattern
- C++ Structural Design Patterns
- C++ Facade Design Pattern
- C++ Iterator Design Pattern
- C++ Mediator Design Pattern
- C++ Memento Design Pattern
- C++ Observer Design Pattern
- C++ State Design Pattern
- C++ Strategy Design Pattern
- C++ Template Method Design Pattern
- C++ Visitor Design Pattern
- C++ Behavioural Design Pattern
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Move Semantics
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ nullptr
- C++ unordered_multiset
- C++ Chain of Responsibility
- C++ Structural Design Patterns
- C++ Adapter Pattern
- C++ Bridge Pattern
- C++ Composite Pattern
- C++ Decorator Pattern
- C++ Command Pattern
- C++ Proxy Pattern
- C++ Useful Resources
- C++ Questions and Answers
- C++ Quick Guide
- C++ Cheatsheet
- C++ STL Tutorial
- C++ Standard Library
- C++ Useful Resources
- C++ Discussion
- C++ Online Compiler
C++ sizeof Operator
The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type.
The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.
Syntax
sizeof (data type)
Where data type is the desired data type including classes, structures, unions, and any other user-defined data type.
sizeof with Primitive Data Types
The sizeof the operator in C++ can be used to determine the size (in bytes) of primitive data types like int, float, double, char, etc. The size of these depends on the system architecture, but common sizes are int (4 bytes), float (4 bytes), double (8 bytes), and char (1 byte) etc.
Lets go through all with an Examples
Example
Heres an example covering all primitive data types with their sizes as an output.
#include <iostream>
int main() {
std::cout << "Size of char: " << sizeof(char) << " bytes\n";
std::cout << "Size of short: " << sizeof(short) << " bytes\n";
std::cout << "Size of int: " << sizeof(int) << " bytes\n";
std::cout << "Size of long: " << sizeof(long) << " bytes\n";
std::cout << "Size of float: " << sizeof(float) << " bytes\n";
std::cout << "Size of double: " << sizeof(double) << " bytes\n";
std::cout << "Size of long double: " << sizeof(long double) << " bytes\n";
std::cout << "Size of bool: " << sizeof(bool) << " bytes\n";
std::cout << "Size of void: " << sizeof(void) << " bytes\n";
// This is not valid and will result in a compile-time error.
return 0;
}
Output
Size of char: 1 bytes Size of short: 2 bytes Size of int: 4 bytes Size of long: 8 bytes Size of float: 4 bytes Size of double: 8 bytes Size of long double: 16 bytes Size of bool: 1 bytes Size of void: 1 bytes
sizeof with Arrays
The sizeof working with arrays depends on whether the array is statically declared (fixed-size) or dynamically allocated (via malloc, new, etc.).
Here we will cover and see the behavior of both arrays in each case.
A statically declared array is an array whose size is fixed at compile time. It is stored in stack memory, where sizeof can give the total size and the number of elements directly.
A dynamically allocated array is created during runtime using malloc(), calloc(), new, or similar memory allocation functions. Here, the array is allocated on the heap, where sizeof only gives the size of the pointer, so you must manually track the number of elements.
Example
#include <iostream>
int main() {
// Static array
int staticArr[4] = {1, 2, 3, 4};
std::cout << "Static array size: " << sizeof(staticArr) << " bytes\n";
std::cout << "Static array elements: " << sizeof(staticArr)
/ sizeof(staticArr[0]) << "\n";
// Dynamic array
int* dynamicArr = new int[4];
std::cout << "Dynamic array pointer size: " << sizeof(dynamicArr) << " bytes\n";
// Clean up
delete[] dynamicArr;
return 0;
}
Output
Static array size: 16 bytes Static array elements: 4 Dynamic array pointer size: 8 bytes
sizeof with Pointers
The sizeof the operator can be used to determine the size of pointers in C++, which represents the amount of memory used to store the pointer itself, not the data it points to.
Example
#include <iostream>
int main() {
int a = 5;
double b = 3.14;
char c = 'A';
std::cout << "Size of pointer to int: " << sizeof(&a) << " bytes\n"
<< "Size of pointer to double: " << sizeof(&b) << " bytes\n"
<< "Size of pointer to char: " << sizeof(&c) << " bytes\n";
int d = 5;
int* ptr = &d;
std::cout << "Size of the data pointed by ptr: " << sizeof(*ptr) <<
" bytes" << std::endl;
return 0;
}
Output
Size of pointer to int: 8 bytes Size of pointer to double: 8 bytes Size of pointer to char: 8 bytes Size of the data pointed by ptr: 4 bytes
sizeof with Structures, Classes, and Unions
The sizeof operator helps to determine the memory size of structures, classes, and unions.
- A structure (struct) groups variables of different types, and its size includes the total size of its members.
- A structure (struct) groups variables of different types, and its size includes the total size of its members.
- A union allows different types to share the same memory space, and its size is determined by the largest member since all members overlap in memory.
Example
#include <iostream>
// Structure Example
struct MyStruct {
char c; // 1 byte
int i; // 4 bytes
double d; // 8 bytes
};
// Class Example
class MyBase {
public:
virtual void foo() {}
};
class MyClass : public MyBase {
public:
char c; // 1 byte
int i; // 4 bytes
double d; // 8 bytes
virtual void bar() {}
};
// Union Example
union MyUnion {
char c; // 1 byte
int i; // 4 bytes
double d; // 8 bytes
};
int main() {
std::cout << "Size of MyStruct: " << sizeof(MyStruct) << " bytes\n";
std::cout << "Size of MyClass: " << sizeof(MyClass) << " bytes\n";
std::cout << "Size of MyUnion: " << sizeof(MyUnion) << " bytes\n";
return 0;
}
Output
Size of MyStruct: 16 bytes Size of MyClass: 24 bytes Size of MyUnion: 8 bytes
Limitation of sizeof
- sizeof does not account for dynamically allocated memory or the memory managed by containers like std::string and std::vector.
- sizeof cannot determine the size of memory for base class objects in derived classes with inheritance.
- sizeof returns the size of a pointer when applied to dynamically allocated memory, not the size of the allocated block.
- The result of sizeof does not include padding added for alignment purposes or the size of the virtual table (vtable) for classes with virtual functions.
- sizeof cannot be used with incomplete types (forward-declared classes or types).