Skip to Main Content
C++ In a Nutshell
book

C++ In a Nutshell

by Ray Lischner
May 2003
Intermediate to advanced content levelIntermediate to advanced
808 pages
32h 24m
English
O'Reilly Media, Inc.
Content preview from C++ In a Nutshell

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>

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++ High Performance

C++ High Performance

Viktor Sehr, Björn Andrist
Optimized C++

Optimized C++

Kurt Guntheroth
Mastering C++ Programming

Mastering C++ Programming

Jeganathan Swaminathan

Publisher Resources

ISBN: 059600298XSupplemental ContentErrata Page