Accidentally sealed and received from the operator sizeof - 1.

#include "stdio.h" int main(void) { printf("%d",sizeof(void)); /// 1 return 0; } 

Can someone explain to me what's the matter?

  • stackoverflow.com/questions/1666224/what-is-the-size-of-void question on SO. They say that in a gcc some kind of crutch, if you don’t assemble with keys at all - pavel
  • This is an error message :) By the way, in Visual C ++ in C mode, a warning (and a value of 0) is issued, and in C ++ mode - an error. - Harry
  • Briefly and cleanly practically, then (at least in gcc) the address arithmetic for void * is the same as for char * . Therefore, since sizeof(char) == 1 , then sizeof(void) == sizeof(char) == 1 . (g ++ for sizeof(void) writes warning, but compiles and the result will be the same - 1) - avp
  • @avp funny thing - MaximPro
  • Well, it's clear that void is something. Yes, we do not know what, but not an empty space (how to represent an empty space in the memory? (Purely hardware)), so we decide that its size is 1 (at least 1, this address should occupy the address). Here, about such reasoning. - avp

2 answers 2

According to standard C (Section 6.2.5 Types)

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

and (6.5.3.4. The sizeof and alignof operators)

This is the size of the operator, that is, the design of a bit-fi eld member. Operator type or an incomplete type.

However, some compilers have their own language extensions that still define the sizeof( void ) expression as having the value 1.

The fact is that the void type did not immediately appear in the C programming language. From Rationale for International Standard β€” Programming Languages ​​— C Section 6.4.1 Keywords

Several keywords were added in C89 : const, enum, signed, void and volatile.

Earlier, the char type played its role For example, if you wanted to copy one object to another using the standard memmove function, the corresponding entry might look like

 memmove( ( char *)obj1, ( char * )obj2, n ); 

This kind of record can still be found in the old programs.

The memmove function memmove has the following declaration

 void *memmove(void *s1, const void *s2, size_t n); 

and, as is known, any pointer can be implicitly cast to the void * type, and therefore no explicit type casting is required for calling this function. You can write just

 memmove( obj1, obj2, n ); 

when using this function with pointers of any type.

And only over time I had introduced the type void .

From Rationale for International Standard β€” Programming Languages ​​— C Section 6.5.4 Cast operators

The new type is void *, which is the same as char *, is therefore preferable for arbitrary pointers

Similar to the char type, since the void * pointers have the same representation as the char * pointers, some compilers have made the size of void equal to 1, since sizeof( *(char *)NULL ) is 1. However, as follows from standard C this type does not have a size, since it is always an incomplete type, and therefore such a construction, like sizeof( void ) , is incorrect, and the compiler should give an error message if its own extensions are disabled when compiling the program.

  • Comments are not intended for extended discussion; conversation moved to chat . - Nofate ♦
  • @Nofate Yes, that really there! Delete the answer itself, since it calls holivar1 - Vlad from Moscow
  • Comments are not deleted, but transferred to the chat. - Nofate ♦
 printf("%lu",sizeof(void)); 

So more accurate. This is the size of the void type.

  • 3
    Ahem ... that's not the question. Do not you confuse what is not 0 for example? - pavel
  • I'm not, and what confuses you? Data type as information must be of some size. - dio4
  • @pavel with zero would not make the difference between the two void * get - Vladimir Gamalyan
  • 2
    @ dio4 and what information does this type store?) - pavel
  • 2
    Information about the value of the void type fits nicely into 0 bits, since there is only one possible value for this type. From here and other restrictions, like a ban on the creation of variables of type void , it is simply meaningless. Therefore, it is really interesting, with what a fright there is not 0. Yes, the accepted answer explains how it happened, but it turns out that this is right in the standard inconsistency, a superfluous "exception". Another question is to whom it prevents and whether it makes sense to correct it. It seems to be all used. And to argue about C design as a language makes little sense :) - D-side