C++ sizeof Keyword
The sizeof
keyword in C++ is used to determine the size, in bytes, of a data type or an object. It is a compile-time operator and is particularly useful for understanding memory usage, ensuring compatibility between platforms, and managing low-level memory operations.
When sizeof
is applied to an object, it returns the size of the object in memory, including any padding bytes added for alignment. Similarly, when used with a type, it provides the size of that type in bytes.
Syntax
</>
Copy
sizeof(expression);
sizeof(type);
- expression
- An object or variable whose size is to be determined.
- type
- The data type for which the size is required.
Examples
Example 1: Using sizeof
with Basic Data Types
In this example, we will use sizeof
to get the sizes of basic data types.
</>
Copy
#include <iostream>
using namespace std;
int main() {
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
cout << "Size of char: " << sizeof(char) << " bytes" << endl;
return 0;
}
Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Explanation:
- The
sizeof
operator is applied to data types directly to get their sizes in bytes. - Output may vary depending on the compiler and platform.
Example 2: Using sizeof
with Variables
In this example, we will show how sizeof
is used to determine the size of variables and objects.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int num = 42;
double pi = 3.14;
char letter = 'A';
cout << "Size of num: " << sizeof(num) << " bytes" << endl;
cout << "Size of pi: " << sizeof(pi) << " bytes" << endl;
cout << "Size of letter: " << sizeof(letter) << " bytes" << endl;
return 0;
}
Output:
Size of num: 4 bytes
Size of pi: 8 bytes
Size of letter: 1 byte
Explanation:
- The size of variables is determined based on their data types.
- The
sizeof
operator evaluates the memory footprint of the specific variable or object.
Example 3: Using sizeof
with Arrays
In this example, we will learn how sizeof
works with arrays.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
cout << "Size of array: " << sizeof(numbers) << " bytes" << endl;
cout << "Size of one element: " << sizeof(numbers[0]) << " bytes" << endl;
cout << "Number of elements: " << sizeof(numbers) / sizeof(numbers[0]) << endl;
return 0;
}
Output:
Size of array: 20 bytes
Size of one element: 4 bytes
Number of elements: 5
Explanation:
- The total size of the array is calculated as the number of elements multiplied by the size of each element.
- The size of one element is determined using
sizeof(numbers[0])
. - The number of elements is calculated by dividing the total array size by the size of one element.
Key Points to Remember about sizeof
Keyword
sizeof
is a compile-time operator that determines the size of a type or an object.- It includes padding bytes used for alignment.
- The result of
sizeof
is always returned as an unsigned integer. - For arrays,
sizeof
returns the total size, not the number of elements. - Output depends on the system architecture and compiler settings.