views:

219

answers:

5

why is sizeof void pointer 2 ?

+6  A: 

The size of a void* is a platform dependent value. Typically it's value is 4 or 8 for 32 an 64 bit platforms respectively. If you are getting 2 as the value then your likely running on a 16 bit coding platform (or potentially have a coding error).

Could you post the code you are using and some more information about your environment / operation system?

JaredPar
He's probably running Turbo C under 16 bit DOS
Paul R
@Paul, thanks updated for 16 bit OS's. I thought it much more likely that it was an error instead of people still using 16 bit OS's but that may be a poor assumption.
JaredPar
@JaredPar: for some reason a lot of colleges (especially those in India) still seem to use things like Turbo C and 16 bit DOS (as well as some very bad text books), which is why we tend to see a lot of questions on SO and elsewhere with e.g. `void main()` and assumptions about sizeof(void *) being 2, etc.
Paul R
A: 

What makes you think it is ? Try compiling and running the following code and i am pretty sure the size won't be 2.

int main(int argc, char** argv)
{
    void *foo = 0;
    printf("%u",sizeof(foo));
}
gilligan
It will be when you run on a 16 bit platform.
Mendelt
size_t isn't usually an int.
Blank Xavier
I realize that combining size_t and %d for int will yield a compiler warning but I suppose it hardly matters in this case. As for the size: yes it will be 2 on a system with 16bit addressing. As he didn't further specifiy the platform that is however unlikely
gilligan
@gilligan, why, he could have downloaded the old TurboC free compiler or be working for an embedded microcontroller. Not unlikely at all.
Amigable Clark Kant
Fix your bogus `printf` call rather than claiming it "hardly matters". This is a site people use to learn, and wrong code detracts from that.
R..
A: 

As JaredPar already pointed out, this is platform dependant. To put it differently: How many bits does the used CPU use for memory-addressing? For 16bit adresses you would get a size of 2 bytes. Are you compiling code for a 16bit microcontroller?

S.C. Madsen
A: 

A pointer stores a memory address that points to something else. The size of a pointer depends on your platform. On a 32 bit platform you need 32 bits or four bytes to store a memory address so sizeof any pointer will return 4.

If sizeof(void*) is 2 you're probably running on a 16 bit platform.

Mendelt
Small mistake, I think you meant to write: "if sizeof(void*) is 2, you're pro.." ;)
lx
@Ix: Thanks, fixed it. Thanks for catching that.
Mendelt
sizeof returns the size with respect to the size of the `char`. The `char` type usually is 8 bit long, but doesn't have to be. I'm not saying I know such a system, but it's not impossible to have a system, where `char` is 32 bit long and in such a system `sizeof(char)`, `sizeof(int)` and `sizeof(void*)` all can be equal to 1.
Maciej Hehl
A: 

Per the online C standard (n1256 draft):

6.2.5 Types
...
27 A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.39) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.

As to why void and char pointers have a size of 2 on your system, I suspect that's because you're on a 16-bit platform.

John Bode