Have you ever wanted to learn how basic types of C++ variables interact in complex situations? Ivor Horton explains this, and also describes some interesting features of C++. This article is from chapter 3 of Ivor Horton's Beginning ANSC C++ The Complete Language (Apress, 2004; ISBN 1590592271).
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.