12

Possible Duplicate:
What is the size of void?

In §6.2.5.19, the prophets let us know that:

The void type comprises an empty set of values

Then why does sizeof(void) yield 1, when 0 seems to suffice?

Community
  • 1
  • 1
Philip
  • 5,795
  • 3
  • 33
  • 68

3 Answers3

20

It is a gcc extension: http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Pointer-Arith.html#Pointer-Arith

In GNU C, addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1.

A consequence of this is that sizeof is also allowed on void and on function types, and returns 1.

The option -Wpointer-arith requests a warning if these extensions are used.

The reason why void needs a size to perform such arithmetics is that ptr - ptr2 does not actually gives you the numeric difference of the addresses but the number of elements the two pointers are apart - and the size of an element pointed to by void *ptr is sizeof(*ptr) which is sizeof(void).

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • If this is an extension, why does `gcc -std=c99 -pedantic` give sizeof(void)==1? -pedantic gives a warning for using sizeof on a void type, but the code compiles and prints 1. – Lundin May 11 '12 at 13:05
  • 3
    @Lundin: because you used `-pedantic` and not `-pedantic-errors` – Christoph May 11 '12 at 13:07
  • 1
    @Christoph In my opinion, it should be sufficient to set the -std=c99 flag and then it shouldn't even allow the code to compile without errors. – Lundin May 11 '12 at 13:08
12

sizeof(void) will not compile on a C compiler.

ISO 9899:2011 6.2.5/19

"The void type comprises an empty set of values; it is an incomplete object type that cannot be completed."

ISO 9899:2011 6.5.3.4/1

"The sizeof operator shall not be applied to an expression that has function type or an incomplete type"

This is normative text: sizeof(void) is not valid C.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • ...and yet it does compile with gcc, since it's a gcc extension. See http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Pointer-Arith.html#Pointer-Arith – Philip May 11 '12 at 13:26
  • 4
    I do wonder how this answers the OP's question... – ThiefMaster May 11 '12 at 13:27
  • @Philip Yes indeed, but it will not compile on a conforming implementation of a C compiler. – Lundin May 11 '12 at 13:28
  • @Lundin: Yep. I mentioned it because I accepted your answer (because it contains actual proof) and wanted it to be as complete as possible. I learned about this gcc extension myself only from the other answers 5 minutes ago ;) – Philip May 11 '12 at 13:31
7

You are probably using gcc, or some other compiler that does this as an extension (in C, sizeof(void) is not valid).

gcc says:

In GNU C, addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1.

A consequence of this is that sizeof is also allowed on void and on function types, and returns 1.

The option -Wpointer-arith requests a warning if these extensions are used.

nos
  • 223,662
  • 58
  • 417
  • 506
  • 1
    ...and it is invalid because of §6.5.3.4.1: "The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member" and §6.2.5.19: "`void` is an "incomplete type that cannot be completed." – Philip May 11 '12 at 13:18