Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is sizeof(void()) a legal expression?

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?

like image 516
skypjack Avatar asked Sep 01 '16 18:09

skypjack


People also ask

Can we calculate the sizeof void?

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.

Why is sizeof void 1?

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).

What is the size of a void data type in C?

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…”

Does changing the size expression affect the result of sizeof?

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 .

Can we use sizeof operator in 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.

What is the sizeof of a void pointer?

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.


1 Answers

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().

like image 102
Some programmer dude Avatar answered Oct 23 '22 12:10

Some programmer dude