More on Handling Basic Data Types - Finding Out About Types
(Page 3 of 13 )
I’ve mentioned several times that the number of bytes used for some types isn’t specified in the C++ standard and that this is therefore set by your compiler. It’s quite possible that you would want to know how many bytes particular types of variables will occupy in your compiler. You could hunt for this information in your compiler’s documentation, but you can also get the information programmatically by using the sizeof operator.
sizeof is a unary operator, so it takes a single operand. It will return an integer value that is a measure of the amount of memory occupied by a particular variable, or by a type. The value returned by sizeof is actually defined as a multiple of the size of type char, but because variables of type char occupy 1 byte, the value returned will be a measure of the number of bytes that the operand occupies.
To obtain the number of bytes occupied by variables of type type, you use the expression sizeof(type). You could therefore output the size of variables of type int with this statement:
std::cout << std::endl
<< "Size of type int is "
<< sizeof(int); // Output the
size of type int
The expression sizeof(int) returns the size of anything declared as type int, and you can find out the size of any data type in this way. To get the size of long double values, you could write this:
std::cout << std::endl
<< "Size of type long double is "
<< sizeof(long double); //Output the size of type long double
You can also apply the sizeof operator to a particular variable, or even to an expression. In this case, the expression doesn’t have to be between parentheses, although you can include them if you wish. Here’s an example that will output the number of bytes occupied by the variable number:
long number = 999999999;
std::cout << std::endl
<< "Size of the variable number is "
<< sizeof number; // Output the size of
a variable
You can treat the value returned by the sizeof operator as an integer, but in fact it’s of type size_t. This isn’t really a new type, though. The word size_t is defined in the standard header as a synonym for one of the fundamental integer types, usually as unsigned int. Because it’s a synonym and not a name for an entity in the standard library, the name is not within the std namespace, so you can use it as is. I can hear your next question already: “What’s the point of having a different type name here? Why not just make it unsigned int?”
The reason for specifying the type returned by the sizeof operator is that it builds in flexibility. The sizeof operator always returns a value of type size_t, which your compiler may well define to be unsigned int, but it doesn’t have to be so. It might be convenient for the developers of a C++ compiler for a particular hardware platform to define size_t to be equivalent to some other integral type, and they’re free to do so. It won’t affect your code, because your assumption is that its type is size_t. You’ll see how to define a synonym for an existing type for yourself later in this chapter. Incidentally, the t in size_t stands for “type,” so the name was chosen to indicate that it is a type for a size.
This article is excerpted from Beginning ANSI C++ The Complete Language by Ivor Horton (Apress, 2004; ISBN 1590592271). Check it out at your favorite bookstore today. Buy this book now. |
Next: Try It Out: Finding the Sizes of Data Types >>
More C++ Articles
More By Apress Publishing