Name
sizeof operator — Size of type operator
Synopsis
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>
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.