C语言 空指针的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3853312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:36:31  来源:igfitidea点击:

sizeof void pointer

cvoid-pointers

提问by alfesani

why is sizeofvoid pointer 2?

为什么是sizeofvoid 指针2

回答by JaredPar

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).

a 的大小void*是平台相关的值。通常,对于 32 位和 64 位平台,它的值分别为 4 或 8 个字节。如果您得到 2 作为值,那么您很可能在 16 位编码平台上运行(或可能存在编码错误)。

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

您能否发布您正在使用的代码以及有关您的环境/操作系统的更多信息?

回答by John Bode

Per the online C standard (n1256 draft):

根据在线 C 标准(n1256 草案)

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.
6.2.5 类型
...
27 指向 void 的指针应具有与指向字符类型的指针相同的表示和对齐要求。39) 同样,指向兼容类型的限定或非限定版本的指针应具有相同的表示和对齐要求。所有指向结构类型的指针都应具有相同的表示和对齐要求。所有指向联合类型的指针都应具有相同的表示和对齐要求。指向其他类型的指针不需要具有相同的表示或对齐要求。

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.

至于为什么 void 和 char 指针在您的系统上的大小为 2,我怀疑这是因为您使用的是 16 位平台。

回答by Mendelt

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.

指针存储指向其他内容的内存地址。指针的大小取决于您的平台。在 32 位平台上,您需要 32 位或 4 个字节来存储内存地址,因此任何指针的 sizeof 都将返回 4。

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

如果 sizeof(void*) 为 2,则您可能在 16 位平台上运行。

回答by S.C. Madsen

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?

正如 JaredPar 已经指出的那样,这取决于平台。换句话说:使用的 CPU 使用多少位进行内存寻址?对于 16 位地址,您将获得 2 个字节的大小。您正在为 16 位微控制器编译代码吗?

回答by Greety_Tobin

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.

Pointer 的大小等于 Integer 的大小。在 16 位编译器中可以是 2 个字节,在 32 位编译器中可以是 4 个字节,在 64 位编译器中可以是 8 个字节。

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

void *ptr,int *ptr并且char *ptr会给你相同的大小,但如果你这样做ptr++,相应的指针将根据那里的数据类型跳转。即 1 个位置 invoidcharcase。同样4个位置的int情况下。