C语言 C中指针的大小是否有所不同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3520059/
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:13:21  来源:igfitidea点击:

Does the size of pointers vary in C?

cpointers

提问by Nils

Possible Duplicates:
Can the Size of Pointers Vary Depending on what’s Pointed To?
Are there are any platforms where pointers to different types have different sizes?

可能的重复:
指针的大小是否会因指向的内容而异?
是否有任何平台指向不同类型的指针具有不同的大小?

Is it possible that the size of a pointer to a float in c differs from a pointer to int? Having tried it out, I get the same result for all kinds of pointers.

c 中指向浮点数的指针的大小与指向 int 的指针的大小是否可能不同?尝试过之后,我对各种指针都得到了相同的结果。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("sizeof(int*): %i\n", sizeof(int*));
    printf("sizeof(float*): %i\n", sizeof(float*));
    printf("sizeof(void*): %i\n", sizeof(void*));
    return 0;
}

Which outputs here (OSX 10.6 64bit)

此处输出哪些(OSX 10.6 64 位)

sizeof(int*): 8
sizeof(float*): 8
sizeof(void*): 8

Can I assume that pointers of different types have the same size (on one arch of course)?

我可以假设不同类型的指针具有相同的大小(当然是在一个拱形上)吗?

回答by Johan Kotlinski

Pointers are not always the same size on the same arch.

指针在同一个拱形上的大小并不总是相同。

You can read more on the concept of "near", "far" and "huge" pointers, just as an example of a case where pointer sizes differ...

您可以阅读有关“近”、“远”和“大”指针概念的更多信息,仅作为指针大小不同的示例...

http://en.wikipedia.org/wiki/Intel_Memory_Model#Pointer_sizes

http://en.wikipedia.org/wiki/Intel_Memory_Model#Pointer_sizes

回答by Carl Smotricz

In days of old, using e.g. Borland C compilers on the DOS platform, there were a total of (I think) 5 memory models which could even be mixed to some extent. Essentially, you had a choice of small or large pointers to data, and small or large pointers to code, and a "tiny" model where code and data had a common address space of (If I remember correctly) 64K.

在过去,例如在 DOS 平台上使用 Borland C 编译器时,总共有(我认为)5 种内存模型,它们甚至可以在某种程度上混合。本质上,您可以选择指向数据的小指针或大指针,以及指向代码的小指针或大指针,以及一个“小”模型,其中代码和数据具有(如果我没记错的话)64K 的公共地址空间。

It was possible to specify "huge" pointers within a program that was otherwise built in the "tiny" model. So in the worst case it was possible to have different sized pointers to the same data typein the same program!

可以在程序中指定“巨大”的指针,否则该程序是在“微小”模型中构建的。所以在最坏的情况下,在同一个程序中可能有不同大小的指针指向相同的数据类型

I think the standard doesn't even forbid this, so theoretically an obscure C compiler could do this even today. But there are doubtless experts who will be able to confirm or correct this.

我认为标准甚至没有禁止这样做,所以理论上一个不起眼的 C 编译器即使在今天也可以做到这一点。但毫无疑问,有专家能够确认或纠正这一点。

回答by Jens Gustedt

Pointers to data must always be compatible with void*so generally they would be nowadays realized as types of the same width.

指向数据的指针必须始终兼容,void*因此通常它们现在会被实现为相同宽度的类型。

This statement is not true for function pointers, they may have different width. For that reason in C99 casting function pointers to void*is undefined behavior.

这个说法不适用于函数指针,它们可能有不同的宽度。出于这个原因,在 C99 中将函数指针转换void*为未定义的行为。

回答by Nigel Harper

As I understand it there is nothing in the C standard which guarantees that pointers to different types must be the same size, so in theory an int * and a float * on the same platform could be different sizes without breaking any rules.

据我了解,C 标准中没有任何内容可以保证指向不同类型的指针必须具有相同的大小,因此理论上同一平台上的 int * 和 float * 可以具有不同的大小而不会违反任何规则。

There is a requirement that char * and void * have the same representation and alignment requirements, and there are various other similar requirements for different subsets of pointer types but there's nothing that encompasses everything.

要求 char * 和 void * 具有相同的表示和对齐要求,并且对于指针类型的不同子集还有各种其他类似的要求,但没有什么可以涵盖所有内容。

In practise you're unlikely to run into any implementation that uses different sized pointers unless you head into some fairly obscure places.

在实践中,除非您进入一些相当晦涩的地方,否则您不太可能遇到使用不同大小指针的任何实现。

回答by JeremyP

I was going to write a reply saying that C99 has various pointer conversion requirements that more or less ensure that pointers to data have to be all the same size. However, on reading them carefully, I realised that C99 is specifically designed to allow pointers to be of different sizes for different types.

我打算写一个回复说 C99 有各种指针转换要求,这些要求或多或少地确保指向数据的指针必须具有相同的大小。然而,仔细阅读它们后,我意识到 C99 是专门设计来允许指针为不同类型的不同大小的。

For instance on an architecture where the integers are 4 bytes and must be 4 byte aligned an int pointer could be two bits smaller than a char or void pointer. Provided the cast actually does the shift in both directions, you're fine with C99. It helpfully says that the result of casting a char pointer to an incorrectly aligned int pointer is undefined.

例如,在整数为 4 字节且必须按 4 字节对齐的体系结构中,int 指针可能比 char 或 void 指针小两位。如果演员实际上在两个方向上都进行了转换,那么 C99 就可以了。它有助于说明将 char 指针转换为不正确对齐的 int 指针的结果是未定义的。

See the C99 standard. Section 6.3.2.3

请参阅C99 标准。第 6.3.2.3 节

回答by MSalters

Yes. It's uncommon, but this would certainly happen on systems that are not byte-addressable. E.g. a 16 bit system with 64 Kword = 128KB of memory. On such systems, you can still have 16 bits int pointers. But a char pointer to an 8 bit char would need an extra bit to indicate highbyte/lowbyte within the word, and thus you'd have 17/32 bits char pointers.

是的。这是不常见的,但这肯定会发生在不可字节寻址的系统上。例如,具有 64 Kword = 128KB 内存的 16 位系统。在这样的系统上,您仍然可以拥有 16 位 int 指针。但是指向 8 位字符的字符指针需要一个额外的位来指示字内的高字节/低字节,因此您将拥有 17/32 位字符指针。

This might sound exotic, but many DSP's spend 99.x% of the time executing specialized numerical code. A sound DSP can be a bit simpler if it all it has to deal with is 16 bits data, leaving the occasional 8 bits math to be emulated by the compiler.

这听起来可能很奇怪,但许多 DSP 花费了 99.x% 的时间来执行专门的数字代码。如果一个声音 DSP 只需要处理 16 位数据,那么它会更简单一些,而偶尔让编译器模拟 8 位数学运算。

回答by Johannes Rudolph

Yes, the size of a pointer is platform dependent. More specifically, the size of a pointer depends on the target processor architecture and the "bit-ness" you compile for.

是的,指针的大小取决于平台。更具体地说,指针的大小取决于目标处理器体系结构和您编译的“位数”。

As a rule of thumb, on a 64bit machine a pointer is usually 64bits, on a 32bit machine usually 32 bits. There are exceptions however.

根据经验,在 64 位机器上,指针通常是 64 位,在 32 位机器上通常是 32 位。不过也有例外。

Since a pointer is just a memory address its always the same size regardless of what the memory it points to contains. So a pointer to a float, a char or an int are all the same size.

由于指针只是一个内存地址,因此无论它指向的内存是什么,它的大小总是相同的。因此,指向 float、char 或 int 的指针的大小都相同。

回答by Dummy00001

Can I assume that pointers of different types have the same size (on one arch of course)?

我可以假设不同类型的指针具有相同的大小(当然是在一个拱形上)吗?

For the platforms with flat memory model (== all popular/modern platforms) pointer size would be the same.

对于具有扁平内存模型的平台(== 所有流行/现代平台),指针大小将相同。

For the platforms with segmented memory model, for efficiency, often there are platform-specific pointer types of different sizes. (E.g. farpointers in the DOS, since 8086 CPU used segmented memory model.) But this is platform specific and non-standard.

对于具有分段内存模型的平台,为了效率,通常会有不同大小的特定于平台的指针类型。(例如far,DOS 中的指针,因为 8086 CPU 使用分段内存模型。)但这是特定于平台的且非标准的。

You probably should keep in mind that in C++ size of normal pointer might differ from size of pointer to virtual method. Pointers to virtual methods has to preserve extra bit of information to not to work properly with polymorphism. This is probably only exception I'm aware of, which is still relevant (since I doubt that segmented memory model would ever make it back).

您可能应该记住,在 C++ 中,普通指针的大小可能与指向虚拟方法的指针的大小不同。指向虚拟方法的指针必须保留额外的信息位,以免无法与多态一起正常工作。这可能是我所知道的唯一例外,这仍然是相关的(因为我怀疑分段内存模型是否会恢复原状)。

回答by Prof. Falken contract breached

Pointers are always the same size on the same arch, regardless of datatype it points to. Otherwise things like casting between different pointer types be useless, and break a lot of things.

指针在同一个拱上的大小总是相同的,不管它指向什么数据类型。否则像在不同指针类型之间进行转换之类的东西是无用的,并且会破坏很多东西。

Many common coding techniques depend on casting between for instance a void pointer (or a char) to various structs, depending on size.

许多常见的编码技术依赖于在例如空指针(或字符)到各种结构之间的转换,具体取决于大小。

Take even the standard printf(), is must be able to take pointers to various kinds of data. If pointers were of different size, implementing printf could get very messy.

即使是标准的printf(),也必须能够获取指向各种数据的指针。如果指针大小不同,则实现 printf 可能会变得非常混乱。

回答by blucz

There are platforms where function pointers are a different size than other pointers.

在某些平台上,函数指针的大小与其他指针的大小不同。

I've never seen more variation than this. All other pointers must be at most sizeof(void*) since the standard requires that they can be cast to void* without loss of information.

我从未见过比这更多的变化。所有其他指针最多必须是 sizeof(void*),因为标准要求它们可以转换为 void* 而不会丢失信息。