From [5.3.3/1], I found that:
The sizeof operator shall not be applied to an expression that has function or incomplete type
From [3.9/5] I found that:
Incompletely-defined object types and cv void are incomplete types
Anyway, for sizeof
does not evaluate it's operands, I would have said that sizeof(void())
was a legal expression (actually GCC compiles it and the result is 1).
On the other side, from here, void
is not mentioned while discussing sizeof
, neither when the types having size 1 are mentioned, nor in the list of the ones having an implementation defined size.
The question is thus: is sizeof(void())
a legal expression?
Is it guaranteed to have size equal to 1?
Or is it a legal expression resulting in an UB and that's all?
The size of void pointer varies system to system. If the system is 16-bit, size of void pointer is 2 bytes. If the system is 32-bit, size of void pointer is 4 bytes. If the system is 64-bit, size of void pointer is 8 bytes.
When you do pointer arithmetic adding or removing one unit means adding or removing the object pointed to size. Thus defining sizeof(void) as 1 helps defining void* as a pointer to byte (untyped memory address).
The void data type doesn’t have a size. If you attempt to take the sizeof void, and the compiler implementation conforms to the standard, you’ll get a compilation error or at least a stern warning, because the standard states that the sizeof operator “ shall not be applied to an…incomplete type…”
If type is a VLA type and changing the value of its size expression would not affect the result of sizeof, it is unspecified whether or not the size expression is evaluated. Except if the type of expression is a VLA, (since C99)expression is not evaluated and the sizeof operator may be used in an integer constant expression .
Except if the type of expression is a VLA, (since C99)expression is not evaluated and the sizeof operator may be used in an integer constant expression . If the type of expression is a variable-length array type, expression is evaluated and the size of the array it evaluates to is calculated at run time.
Now, the sizeof a void pointer (i.e., void *) is the size of a pointer, which varies from one compiler implementation to another, and from one target platform to another in the same compiler implementation.
void()
is a function type (it's a function which takes no arguments and returns nothing), so it's not a valid type in sizeof()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With