what's the mechanism of sizeof() in C/C++? - Stack Overflow
How sizeof works in C/C++
Does sizeof return the number of bytes or the number of octets of a type in C? - Stack Overflow
Confuso da sizeof integer in C
Videos
You know, there's a reason why there are standard documents (3.8MB PDF); C99, section 6.5.3.4, §2:
The
sizeofoperator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
In response to ibread's comment, here's an example for the C99 variable length array case:
Copy#include <stdio.h>
size_t sizeof_int_vla(size_t count)
{
int foo[count];
return sizeof foo;
}
int main(void)
{
printf("%u", (unsigned)sizeof_int_vla(3));
}
The size of foo is no longer known at compile-time and has to be determined at run-time. The generated assembly looks quite weird, so don't ask me about implementation details...
sizeof is an operator, not a function.
It's usually evaluated as compile time - the exception being when it's used on C99-style variable length arrays.
Your example is evaluating sizeof(int), which is of course known at compile time, so the code is replaced with a constant and therefore the ++ doesn't exist at run-time to be executed.
Copyint i=0;
cout << sizeof(++i) << endl;
cout << i << endl;
It's also worth noting that since it's an operator, it can be used without the brackets on values:
Copyint myVal;
cout << sizeof myVal << endl;
cout << sizeof(myVal) << endl;
Are equivalent.
Answer: sizeof returns the size of the type in bytes.
Example: sizeof(char) is 100% guaranteed to be 1, but this does not mean, that it's one octet (8 bits).
Proved by the standard:
in 6.5.3.4, point 2:
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
...
When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. When applied to an operand that has array type, the result is the total number of bytes in the array) When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.
Also, in Section 3.6, point 3:
A byte is composed of a contiguous sequence of bits, the number of which is implementation-defined
sizeof always returns size as the number of bytes. But according to wikipedia:
In the programming languages C and C++, the unary operator sizeof is used to calculate the size of any datatype, measured in the number of bytes required to represent the type. A byte in this context is the same as an unsigned char, and may be larger than 8 bits, although that is uncommon.