C++: sizeof operator

It's sometimes necessary to know how much memory is used for variables. Because the C standard was written after there were C compilers, the size of types was already different in different compilers and on different machine architectures. Therefore there is no single definition of how big an int is, for example.

If it's necessary to find out how much memory a type uses, you can use the sizeof operator, which returns the amount of memory need in bytes. (8 bits per byte) Altho it is technically an operator, everyone writes it as if it was a function. For example,

  cout << sizeof(int);
would print 4 on many current compilers because that is the most common (Dev C++, MS Visual Studio, Borland C++Builder, gcc, ...). In fact, many programs simply make some assumptions about the size of variables. It is common to assume they are 32, but if you want to write for maximum portability, it is better to use sizeof.

The single operand (argument) to sizeof is either a type or a variable.