C++ Programming

From Wikibooks, open books for an open world
Jump to: navigation, search

The sizeof keyword refers to an operator that works at compile time to report on the size of the storage occupied by a type of the argument passed to it (equivalently, by a variable of that type). That size is returned as a multiple of the size of a char, which on many personal computers is 1 byte (or 8 bits). The number of bits in a char is stored in the CHAR_BIT constant defined in the <climits> header file. This is one of the operators for which operator overloading is not allowed.

//Examples of sizeof use
int int_size( sizeof( int ) );// Might give 1, 2, 4, 8 or other values.
 
// or
 
int answer( 42 );
int answer_size( sizeof( answer ) );// Same value as sizeof( int )
int answer_size( sizeof answer);    // Equivalent syntax

For example, the following code uses sizeof to display the sizes of a number of variables:

 
    struct EmployeeRecord {
      int ID;
      int age;
      double salary;
      EmployeeRecord* boss;
    };
 
    //...
 
    cout << "sizeof(int): " << sizeof(int) << endl
         << "sizeof(float): " << sizeof(float) << endl
         << "sizeof(double): " << sizeof(double) << endl
         << "sizeof(char): " << sizeof(char) << endl
         << "sizeof(EmployeeRecord): " << sizeof(EmployeeRecord) << endl;
 
    int i;
    float f;
    double d;
    char c;
    EmployeeRecord er;
 
    cout << "sizeof(i): " << sizeof(i) << endl
         << "sizeof(f): " << sizeof(f) << endl
         << "sizeof(d): " << sizeof(d) << endl
         << "sizeof(c): " << sizeof(c) << endl
         << "sizeof(er): " << sizeof(er) << endl;

On most machines (considering the size of char), the above code displays this output:

 
    sizeof(int): 4
    sizeof(float): 4
    sizeof(double): 8
    sizeof(char): 1
    sizeof(EmployeeRecord): 20
    sizeof(i): 4
    sizeof(f): 4
    sizeof(d): 8
    sizeof(c): 1
    sizeof(er): 20

It is also important to note that the sizes of various types of variables can change depending on what system you're on. Check the data types page for more information.

Syntactically, sizeof appears like a function call when taking the size of a type, but may be used without parentheses when taking the size of a variable type (e.g. sizeof(int)). Parentheses can be left out if the argument is a variable or array (e.g. sizeof x, sizeof myArray). Style guidelines vary on whether using the latitude to omit parentheses in the latter case is desirable.

Consider the next example:

    #include <cstdio>
 
    short func( short x )
    {
      printf( "%d", x );
      return x;
    }
 
    int main()
    {
      printf( "%d", sizeof( sizeof( func(256) ) ) );
    }

Since sizeof does not evaluate anything at run time, the func() function is never called. All information needed is the return type of the function, the first sizeof will return the size of a short (the return type of the function) as the value 2 (in size_t, an integral type defined in the include file STDDEF.H) and the second sizeof will return 4 (the size of size_t returned by the first sizeof).

sizeof measures the size of an object in the simple sense of a contiguous area of storage; for types which include pointers to other storage, the indirect storage is not included in the value returned by sizeof. A common mistake made by programming newcomers working with C++ is to try to use sizeof to determine the length of a string; the std::strlen or std::string::length functions are more appropriate for that task.

sizeof has also found new life in recent years in template meta programming, where the fact that it can turn types into numbers, albeit in a primitive manner, is often useful, given that the template metaprogramming environment typically does most of its calculations with types.