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?
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-ο¬ 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.
printf("%lu",sizeof(void)); So more accurate. This is the size of the void type.
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-sideSource: https://ru.stackoverflow.com/questions/611541/
All Articles
void *is the same as forchar *. Therefore, sincesizeof(char) == 1, thensizeof(void) == sizeof(char) == 1. (g ++ forsizeof(void)writes warning, but compiles and the result will be the same - 1) - avp