Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof(void) equals 1 in C? [duplicate]

Tags:

c

void

sizeof

Possible Duplicate:
What is the size of void?

Hi all ! I am using gcc for compiling my C programs, just discovered accidentally that the sizeof(void) is 1 byte in C.

Is there any explanation for this ? I always thought it to be ZERO (if it really stores nothing) !

Thanks !

like image 741
Raj Avatar asked Jul 28 '10 07:07

Raj


2 Answers

This is a non standard extension of gcc, but has a rationale. When you do pointer arithmetic adding or removing one unit means adding or removing the object pointed to size. Thus defining sizeof(void) as 1 helps defining void* as a pointer to byte (untyped memory address). Otherwise you would have surprising behaviors using pointer arithmetic like p+1 == p when p is void*.

The standard way would be to use `char* for that kind of purpose (pointer to byte).

like image 176
kriss Avatar answered Sep 28 '22 00:09

kriss


this is a gcc specific feature - see here http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Pointer-Arith.html#Pointer-Arith

or

What is the size of void?

like image 37
Nikolaus Gradwohl Avatar answered Sep 28 '22 02:09

Nikolaus Gradwohl