tags:

views:

1449

answers:

7

What would this statement yield?

void * p = (void*) 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.

+17  A: 

void has no size, that would be a compilation error. For the same reason you can't do something like:

void n;

EDIT. To my surprise, doing sizeof(void) actually does compile in C:

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

However, in C++ it does not:

$ 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
reko_t
But GCC has an option to treat 'sizeof(void) == sizeof(char)'...See -Wpointer-arith
Jonathan Leffler
+1  A: 

In C, sizeof(void) == 1 in GCC, but this appears to depend 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.
Greg Hewgill
Only in GCC is sizeof(void) == 1; in the standard, it is undefined behaviour.
Jonathan Leffler
Updated to add GCC specific note.
Greg Hewgill
+8  A: 

Although void may stand in place for a type, it cannot actually hold a value. Therefore, it has no size in memory. Getting the size of a void isn’t defined.

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

Konrad Rudolph
+2  A: 

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

Aleksei Potov
+12  A: 

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.

hrnt
The option `-Wpointer-arith` is implied by `-pedantic`. I always used `-pedantic`. :)
jleedev
@jleedev Indeed, I also use -pedantic as much as I can. Unfortunately, though, some third party library headers make gcc -pedantic very sad :(
hrnt
+3  A: 

Taking the size of void is a GCC extension.

pmg
+1  A: 

To the 2nd part of the question: Note that sizeof(void *)!= sizeof(void). On a 32-bit arch, sizeof(void *) is 4 bytes, so p++, would be set accordingly.The amount by which a pointer is incremented is dependent on the data it is pointing to. So, it will be increased by 1 byte.

Amit
so p was pointing to 1 byte data since sizeof(void) gives 1 so p++ means p would be increment by 1 and not 4??
Lakshmi
The amount by which a pointer is incremented is dependent on the data it is pointing to. So Yes.
Amit
p is of void* (void pointer) type, not void. So it would be incremented by the size of void* type (which is usually 4 in 32 bit systems).
Technowise
@Technowise: no. Any pointer of type `T*` is incremented by `sizeof(T)` (and *not* `sizeof(T*)`, which would almost always be the same, except *perhaps* for function pointers) when it is incremented. The exception is of course `void` (and again the exception is when having the GCC extensions enabled).
Konrad Rudolph