指针的大小和体系结构 [英] size of pointers and architecture

查看:86
本文介绍了指针的大小和体系结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过在普通台式PC上运行简单的C ++程序进行基本测试,似乎可以假设任何类型的指针(包括函数指针)的大小等于目标体系结构位的大小吗?

By conducting a basic test by running a simple C++ program on a normal desktop PC it seems plausible to suppose that sizes of pointers of any type (including pointers to functions) are equal to the target architecture bits ?

例如:在32位体系结构中-> 4个字节,在64位体系结构中-> 8个字节.

For example: in 32 bits architectures -> 4 bytes and in 64 bits architectures -> 8 bytes.

但是我记得读过那本书,但总的来说却不是这样!

However I remember reading that, it is not like that in general!

所以我想知道会是什么情况?

So I was wondering what would be such circumstances?

  • 用于使指向数据类型的指针的大小与指针的大小相等 其他数据类型
  • 为了使指向数据类型的指针的大小与指针的大小相等 功能
  • 为了使指向目标体系结构的指针大小相等
  • For equality of size of pointers to data types compared with size of pointers to other data types
  • For equality of size of pointers to data types compared with size of pointers to functions
  • For equality of size of pointers to target architecture

推荐答案

不,这是不合理的.进行此假设可能会导致错误.

No, it is not reasonable to assume. Making this assumption can cause bugs.

C或C ++中指针(和整数类型)的大小最终由C或C ++实现确定.普通的C或C ++实现受其目标体系结构和操作系统的影响很大,但是它们可能出于执行速度以外的原因而选择其类型的大小,例如支持较小的内存使用,支持未写入代码的目标.完全可以移植到任何类型的大小,或者支持更轻松地使用大整数.

The sizes of pointers (and of integer types) in C or C++ are ultimately determined by the C or C++ implementation. Normal C or C++ implementations are heavily influenced by the architectures and the operating systems they target, but they may choose the sizes of their types for reasons other than execution speed, such as goals of supporting smaller memory use, supporting code that was not written to be fully portable to any type sizes, or supporting easier use of big integers.

我已经看到了针对64位系统但提供32位指针的编译器,目的是构建使用较小内存的程序. (已经观察到,指针的大小是内存消耗的一个重要因素,这是由于使用了许多具有许多连接的结构以及使用指针的引用.)在假设指针大小等于64位寄存器的情况下编写的源代码大小会破.

I have seen a compiler targeted for a 64-bit system but providing 32-bit pointers, for the purpose of building programs with smaller memory use. (It had been observed that the sizes of pointers were a considerable factor in memory consumption, due to the use of many structures with many connections and references using pointers.) Source code written with the assumption that the pointer size equalled the 64-bit register size would break.

这篇关于指针的大小和体系结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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