开发者

sizeof void pointer

开发者 https://www.devze.com 2022-12-07 21:45 出处:网络
wh开发者_Python百科y is sizeof void pointer 2 ?The size of a void* is a platform dependent value.Typically it\'s value is 4 or 8 bytes for 32 and 64 bit platforms respectively.If you are getting 2 as

wh开发者_Python百科y is sizeof void pointer 2 ?


The size of a void* is a platform dependent value. Typically it's value is 4 or 8 bytes for 32 and 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 / operating system?


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.


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.


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?


Size of a Pointer is equal to the size of an Integer . It can be 2 bytes in a 16bit compiler and 4 bytes in 32 bit compiler and 8 in 64 bit compiler.

void *ptr, int *ptr and char *ptr will give you same size but if you do ptr++ , the corresponding pointer will jump according to there data types. ie 1 position in void and char case. Similarly 4 positions in int case.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号