Alex Rivera | Logout

What is the size of void?

Asked 2009-11-03T09:30:11.360
92

What would this statement yield?

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

Edit: An extension to the question.

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

Edit
Report

2 Answers

50

If you are using GCC and you are not using compilation flags that remove compiler specific extensions, then sizeof(void) is 1. GCC has a nonstandard extension that does that.

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

answered 2009-11-03T09:35:35.800
-1

while sizeof(void) perhaps makes no sense in itself, it is important when you're doing any pointer math.

eg.

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

If sizeof(void) is considered 1 then this will work. If sizeof(void) is considered 0 then you hit an infinite loop.

answered 2011-11-17T19:59:03.910

Your Answer