sizeof

From Cppwiki

Jump to: navigation, search

sizeof returns the size, in bytes (chars), of its argument. sizeof does not evaluate its argument; the result is known at compile time. sizeof returns an std::size_t (declared in <cstdlib>).

For example, on an implementation with 4-byte integers:

 std::size_t n = sizeof(int); // n = 4
 int i = 0; n = sizeof(i); // n = 4
 n = sizeof(i++); // return type of i++ is int, so n = 4.
                  // the value of i is still 0, because sizeof does not evaluate its argument
 int *p; n = sizeof(*p); // n = 4, because the type of *p is int.  the pointer does not need to be initialised,
                         // because sizeof does not evaluate its argument, so no undefined behaviour occurs.

The value of sizeof(char) is always 1, because sizeof is expressed in bytes, and a char is the same size as a byte. To find the size of char, in bits, use CHAR_BIT.

Personal tools