What is the size of a pointer? What exactly does it depend on?(指针的大小是多少?它具体取决于什么?)
问题描述
我在网上搜索,虽然可以找到一些讨论,但没有找到全面的描述.因此,如果任何人都可以形成一个涵盖有关指针大小的所有内容的答案,那将有很大帮助.答案至少应涵盖以下主题
I searched online and while I could find a few discussions, I did not find a comprehensive description. So if anyone could form an answer which covers everything about size of a pointer, it would be of great help. The answer should at least cover following topics
- 指针的大小取决于什么?
- 架构的什么特性会影响指针的大小?(详细)
- 编译器如何影响指针的大小?
推荐答案
指针是高级语言提供的抽象;理论上它可以是任何宽度.这完全是编译器的心血来潮.
A pointer is an abstraction provided by a high-level language; in theory it could be any width at all. It's totally at the whim of the compiler.
在实践中,它通常与底层硬件的内存地址的宽度有关,因为这通常是编译器实现的最有效的东西.不过也有例外;例如,C++ 的成员函数指针没有直接映射到硬件地址,因为它需要表示两个实体(函数和类型的一些概念).
In practice, it's typically related to the width of the memory addresses of the underlying hardware, as that's usually the most efficient thing for the compiler to implement. There are exceptions though; for example, C++'s pointer-to-member-function does not have a direct mapping to hardware addresses, as it needs to represent two entities (the function and some notion of the type).
然而,即使撇开这一点,仍然存在复杂性.例如:
However, even leaving that aside, there are still complexities. For example:
- 在大多数现代硬件上,您的程序将使用虚拟内存地址,而不是物理地址(它们的宽度可能不同).除非您正在编写内核空间代码.
- 在某些架构(例如 x86)上,底层硬件展示了一个分段地址空间.这确实很复杂,但大部分都被操作系统和虚拟内存系统抽象掉了.但是,如果您正在为非常老的 x86 编写内核空间代码或代码,则必须处理它.
- 在当前的 x86-64 上,(虚拟)内存地址实际上只有 48 位宽.
- x86-64 支持 32 位和 64 位可执行文件.
- 您可能正在一个虚拟机中运行,该虚拟机可以根据需要执行任何操作(相对于底层物理机).
- On most modern hardware, your program will work with virtual memory addresses, rather than physical addresses (which may not be the same width). Unless you're writing kernel-space code.
- On some architectures (e.g. x86), the underlying hardware exhibits a segmented address space. This is really complicated, but is mostly abstracted away by the OS and the virtual-memory system. If you're writing kernel-space code or code for really old x86s, you'll have to deal with it, though.
- On current x86-64, (virtual) memory addresses are actually only 48-bits wide.
- x86-64 supports both 32-bit and 64-bit executables.
- You may be running inside a virtual machine, which again can do whatever if it wants (relative to the underlying physical machine).
这篇关于指针的大小是多少?它具体取决于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:指针的大小是多少?它具体取决于什么?
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01