相同架构上数据类型的指针的sizeof指针有所不同 [英] sizeof Pointer differs for data type on same architecture

查看:174
本文介绍了相同架构上数据类型的指针的sizeof指针有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览一些帖子,并注意到根据sizeof,指针的大小可以不同,具体取决于编译和运行代码的体系结构.对我来说似乎足够合理(即:32位体系结构上的4字节指针,64位体系结构上的8字节指针很有意义).

I have been going through some posts and noticed that pointers can be different sizes according to sizeof depending on the architecture the code is compiled for and running on. Seems reasonable enough to me (ie: 4-byte pointers on 32-bit architectures, 8-byte on 64-bit, makes total sense).

令我惊讶的一件事是,指针的大小可以根据其指向的数据类型而有所不同.我以为在32位体系结构上,所有指针的大小均为4个字节,但事实证明,函数指针的大小可以不同(即:比我期望的要大).为什么使用C编程语言?我找到了一篇文章,针对C ++进行了解释,以及

One thing that surprises me is that the size of a pointer can different based on the data type it points to. I would have assumed that, on a 32-bit architecture, all pointers would be 4-bytes in size, but it turns out that function pointers can be a different size (ie: larger than what I would have expected). Why is this, in the C programming language? I found an article that explains this for C++, and how the program may have to cope with virtual functions, but this doesn't seem to apply in pure C. Also, it seems the use of "far" and "near" pointers is no longer necessary, so I don't see those entering the equation.

那么,在C语言中,什么理由,标准或文档描述了为什么并非所有指针在同一体系结构上都具有相同的大小?

So, in C, what justification, standard, or documentation describes why not all pointers are the same size on the same architecture?

谢谢!

推荐答案

C标准针对要求的内容制定了法律:

The C standard lays down the law on what's required:

  • 所有数据指针都可以转换为void*并返回,而不会丢失信息.
  • 所有struct指针都具有相同的表示形式+对齐方式,因此可以相互转换.
  • 所有union指针都具有相同的表示形式+对齐方式,因此可以相互转换.
  • 所有字符指针和void指针都具有相同的表示形式+对齐方式.
  • 所有指向合格和不合格兼容类型的指针都应具有相同的表示形式+对齐方式. (例如,相同类型的未签名/已签名版本兼容)

  • All data pointers can be converted to void* and back without loss of information.
  • All struct-pointers have the same representation+alignment and can thus be converted to each other.
  • All union-pointers have the same representation+alignment and can thus be converted to each other.
  • All character pointers and void pointers have the same representation+alignment.
  • All pointers to qualified and unqualified compatible types shall have the same representation+alignment. (For example unsigned / signed versions of the same type are compatible)

所有函数指针具有相同的表示形式和对齐方式,可以转换为任何其他函数指针类型,然后再次转换.

All function pointers have the same representation+alignment and can be converted to any other function pointer type and back again.

不需要更多.
委员会通过检查所有当前的实现和机器并尽可能多地编纂保证来得出这些保证.

Nothing more is required.
The committee arrived at these guarantees by examining all current implementations and machines and codifying as many guarantees as they could.

在指针自然是单词指针而不是字符指针的体系结构上,您将获得不同大小的数据指针.
在具有不同大小的代码/数据空间的体系结构(许多微处理器)上,或者在需要正确调用函数的附加信息(例如itanium,尽管它们通常将其隐藏在数据指针后面)的情况下,您将获得不同大小的代码指针.数据指针.

On architectures where pointers are naturally word pointers instead of character pointers, you get data pointers of different sizes.
On architectures with different size code / data spaces (many micro-processors), or where additional info is needed for properly invoking functions (like itanium, though they often hide that behind a data-pointer), you get code pointers of different size from data pointers.

这篇关于相同架构上数据类型的指针的sizeof指针有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆