DekGenius.com
Team LiB   Previous Section   Next Section
sizeof operator Size of type operator

unary-expr := sizeof ( type-id ) | sizeof unary-expr

At compile time, the sizeof operator returns the amount of memory required to hold an object whose type is type-id or the type of unary-expr. In the latter case, unary-expr is not evaluated. The size of a type includes any padding that the compiler adds to it, so the size of an array of N elements is always equal to N times the size of a single element.

By definition, sizeof(char) is 1, so you can think of the size of other types as multiples of the size of a character. The expression type is std::size_t.

Example

class point { ... };
point* p = malloc(sizeof(point));
point corners[] = { { 2, 4 }, {4, 2}, ..., { 42, 10 } };
const unsigned count = sizeof(corners) / sizeof(corners[0]);

See Also

expression, type, Chapter 3, <cstdlib>

    Team LiB   Previous Section   Next Section