What is the size of the void?

What will this operator give?

void * p = (void*) malloc(sizeof(void)); 



Edit: question extension.

If sizeof (void) gives 1 in the GCC compiler, then 1 byte of memory is allocated, and the pointer p points to this byte and will p ++ increase to 0x2346? Suppose p was 0x2345. I am talking about p and not * p.

+60
c ++ c
Nov 03 '09 at 9:30
source share
10 answers

The void type has no size; it will be a compilation error. For the same reason, you cannot do something like:

 void n; 

EDIT. To my surprise, executing sizeof(void) really compiles in GNU C:

 $ echo 'int main() { printf("%d", sizeof(void)); }' | gcc -xc -w - && ./a.out 1 

However, in C ++ this is not the case:

 $ echo 'int main() { printf("%d", sizeof(void)); }' | gcc -xc++ -w - && ./a.out <stdin>: In function 'int main()': <stdin>:1: error: invalid application of 'sizeof' to a void type <stdin>:1: error: 'printf' was not declared in this scope 
+44
Nov 03 '09 at 9:31
source share

If you use GCC, and you do not use compilation flags that remove specific extensions for the compiler, then sizeof(void) is 1. GCC has a non-standard extension that does this.

In general, void is an incomplete type, and you cannot use sizeof for incomplete types.

+35
Nov 03 '09 at 9:35
source share

Although void can stand still for a type, it actually cannot hold a value. Therefore, it has no size in memory. Getting void size is not defined.

A void A pointer is just a language construct meaning a pointer to untyped memory.

+15
Nov 03 '09 at 9:33
source share

Taking the void size is an extension of GCC .

+9
Nov 03 '09 at 9:39
source share

void has no size. In C and C ++, the expression sizeof (void) invalid.

In C, quoting N1570 6.5.3.4, paragraph 1:

The sizeof operator does not apply to an expression that has a function type or an incomplete type, to a parenthesized name such a type or expression that denotes a member of a bit field.

(N1570 is a draft of the ISO C standard of 2011.)

void is an incomplete type. This paragraph is a limitation, meaning that any relevant C compiler must diagnose any violation of it. (A diagnostic message may be a warning, not immaterial.)

The C ++ 11 standard has a very similar wording. Both issues were published after this question was asked, but the rules go back to the 1989 ANSI C standard and the earliest C ++ standards. In fact, the rule that void is an incomplete type that sizeof cannot be applied to is returned just like introducing void into the language.

gcc has an extension that treats sizeof (void) as 1. gcc is not a compatible C compiler by default, so in default mode it does not warn about sizeof (void) . Extensions like this are allowed even for fully compatible C compilers, but diagnostics are still required.

+6
Feb 01 '15 at 3:17
source share

sizeof() cannot be applied to incomplete types. And void is an incomplete type that cannot be completed.

+5
Nov 03 '09 at 9:34
source share

In C, sizeof(void) == 1 in GCC, but it depends on your compiler.

In C ++, I get:

 In function 'int main ()':
 Line 2: error: invalid application of 'sizeof' to a void type
 compilation terminated due to -Wfatal-errors.
+3
Nov 03 '09 at 9:33
source share

In the second part of the question: Note that sizeof (void *)! = Sizeof (void). In a 32-bit arc, sizeof (void *) is 4 bytes, so p ++ will be set accordingly. The value by which the pointer increases depends on the data it points to. Thus, it will be increased by 1 byte.

0
Nov 03 '09 at 9:57
source share

while sizeof (void) may not make sense on its own, it is important when you do pointer math.

eg.

  void *p; while(...) p++; 

If sizeof (void) is considered 1, this will work. If sizeof (void) is considered 0, you get into an infinite loop.

-one
Nov 17 '11 at 19:59
source share

Most C ++ compilers have chosen to increase compilation when trying to get sizeof(void) .

When compiling C, gcc did not match and decided to define sizeof(void) as 1. This may look strange, but it has a logical rationale. When you do pointer arithmetic by adding or removing one unit, it means adding or removing an object that points to the size. Thus, defining sizeof(void) as 1 helps to define void* as a pointer to a byte (untyped memory address). Otherwise, you would have amazing behavior using pointer arithmetic, for example p+1 == p when p is void* . Such pointer arithmetic on void pointers is not allowed in C ++, but works fine when compiling C with gcc.

The standard recommended way would be to use char* for this purpose (pointer to byte).

Another similar difference between C and C ++ when using sizeof occurs when you define an empty structure:

 struct Empty { } empty; 

Using gcc as my C compiler sizeof(empty) returns 0. Using g ++, the same code will return 1.

I'm not sure what the C and C ++ standards indicate at the moment, but I believe that sizing some empty structures / objects helps with link management to avoid two links to different sequential objects, the first of which is empty, get the same address. If the link is implemented using hidden pointers, as is often done, ensuring that another address helps them compare.

But this simply avoids unexpected behavior (comparing link examples) by introducing another (empty objects, even PODs consume at least 1 byte of memory).

-one
Mar 21 '13 at 9:34
source share



All Articles