The sizeof operator is used to determine how many bytes of memory an object or a type takes up.
It gets resolved at compile time, therefore the code involved needs no runtime calculation.
A type name can be passed to the sizeof operator.
#include <iostream>
int main()
{
std::cout << "Size of char: "
// Query the size in bytes without evaluating the operand
<< sizeof(char)
<< " bytes\n";
std::cout << "Size of int: "
<< sizeof(int)
<< " bytes\n";
std::cout << "Size of double: "
<< sizeof(double)
<< " bytes\n";
}
It returns the size in bytes of one object of that type.
The result depends on your system, but char is always one byte.
It can be passed an expression and it returns how many bytes the expression takes up according to its type.
#include <iostream>
int main()
{
int number{42};
double price{19.99};
char letter{'A'};
std::cout << "Size of number: "
<< sizeof(number)
<< " bytes\n";
std::cout << "Size of price: "
<< sizeof(price)
<< " bytes\n";
std::cout << "Size of letter: "
<< sizeof(letter)
<< " bytes\n";
}
So, sizeof(number) will produce exactly the same result as sizeof(int) will if the variable number is of type int. Whatever value is in the variable does not matter.
The result of using sizeof is always a std::size_t, which is an unsigned integer type defined by the standard library.
#include <cstddef>
#include <iostream>
int main()
{
// Initialize int_size with the value returned by sizeof
std::size_t int_size{sizeof(int)};
std::size_t double_size{sizeof(double)};
std::cout << "int occupies "
<< int_size
<< " bytes\n";
std::cout << "double occupies "
<< double_size
<< " bytes\n";
}
It is good practice to store the value in a variable of this type as it can easily be added to other size values.
Multiple instances of the sizeof operator can be combined by storing their results in different std::size_t variables and then adding those together to calculate total combined sizes.
#include <cstddef>
#include <iostream>
int main()
{
int value{100};
// Initialize this floating-point value with a decimal fraction
double rate{0.05};
std::size_t bytes_for_value{sizeof(value)};
std::size_t bytes_for_rate{sizeof(rate)};
std::size_t total_bytes{
bytes_for_value + bytes_for_rate
};
std::cout << "Bytes used by value: "
<< bytes_for_value
<< '\n';
std::cout << "Bytes used by rate: "
<< bytes_for_rate
<< '\n';
std::cout << "Total: "
<< total_bytes
<< '\n';
}
Each instance is evaluated at compile time, and so the actual size calculation does not take place during the execution of the program but during compilation.
Exercise: The sizeof OperatorStarter code, hints and solutionOpenClose
Practice: The sizeof Operator
Work through the mission, reveal hints only when needed, and compare your result with the model solution.
Your challenge
Change value from 100 to 10000 and add a check comparing sizeof(value) with sizeof(int). Explain why a larger decimal value does or does not require more bytes for this variable.
Mission checklist
- Run the starter as a baseline, then predict the change requested in Your challenge.
- Make that change in your editor; use the lesson-specific hints if you need help.
- Compile and run your answer, then compare with the worked solution and explain the result.
Starter program
This is the working baseline, not the completed answer. Compile as C++20, then make the change described above.
#include <cstddef>
#include <iostream>
int main()
{
int value{100};
// Initialize this floating-point value with a decimal fraction
double rate{0.05};
std::size_t bytes_for_value{sizeof(value)};
std::size_t bytes_for_rate{sizeof(rate)};
std::size_t total_bytes{
bytes_for_value + bytes_for_rate
};
std::cout << "Bytes used by value: "
<< bytes_for_value
<< '\n';
std::cout << "Bytes used by rate: "
<< bytes_for_rate
<< '\n';
std::cout << "Total: "
<< total_bytes
<< '\n';
}Hints
sizeof(value) measures the size of an int object, not the number of digits in 100. sizeof(rate) similarly depends on the double type.
Use sizeof(value) == sizeof(int) as a Boolean expression. Keep value declared as int when changing its initializer.
Worked solution
value is now 10000, but its type is still int. sizeof depends on the object type, not the number of decimal digits in its value. The added comparison confirms sizeof(value) equals sizeof(int); the actual byte counts are platform-specific.
#include <cstddef>
#include <iostream>
int main()
{
int value{10000};
// Initialize this floating-point value with a decimal fraction
double rate{0.05};
std::size_t bytes_for_value{sizeof(value)};
std::size_t bytes_for_rate{sizeof(rate)};
std::size_t total_bytes{
bytes_for_value + bytes_for_rate
};
std::cout << "Bytes used by value: "
<< bytes_for_value
<< '\n';
std::cout << "Bytes used by rate: "
<< bytes_for_rate
<< '\n';
std::cout << "Total: "
<< total_bytes
<< '\n';
std::cout << "Matches int size: " << (sizeof(value) == sizeof(int)) << '\n';
}
Example output
This is one tested C++20 run. Sizes, capacity, formatting, floating-point details, or extra container lifetime messages may differ by implementation; use the explanation to identify the guaranteed result.
Bytes used by value: 4
Bytes used by rate: 8
Total: 12
Matches int size: 1
0 comments
No comments yet. Be the first.