什么是指针的大小?究竟是依靠什么? [英] What is the size of a pointer? What exactly does it depend on?

查看:123
本文介绍了什么是指针的大小?究竟是依靠什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上搜索,虽然我能找到一些讨论,我没有找到一个COM prehensive描述。因此,如果任何人都可以形成占地约一个指针的大小一切的答案,这将是很大的帮助。答案至少应覆盖以下主题


  1. 什么是一个指针的大小取决于?

  2. 什么功能架构的影响指针的大小? (详细)

  3. 编译器如何影响指针的大小?


解决方案

一个指针是一个高层次的语言提供了一个抽象的;在理论上它可以是任何宽度的。这是完全在编译器的心血来潮。

在实践中,它通常与底层硬件的存储器地址的宽度,因为这是通常的编译器来实现最有效的事。但也有例外,虽然;例如,C ++的指针到成员函数不具有直接映射到硬件地址,因为它需要重新present两个实体(功能和类型的一些概念)。

然而,即使在离开那个之外,还有复杂性。例如:


  • 在最现代化的硬件,你的程序将使用的虚拟的内存地址,而不是物理地址(这可能不是相同的宽度)工作。除非你正在编写内核空间code。

  • 在某些体系结构(如86),底层硬件表现出的分段的地址空间。这是非常复杂的,而是由操作系统和虚拟内存系统大多是抽象化了。如果你正在编写内核空间code或code为真的老了x86s,你必须处理它,但。

  • 在当前的x86-64,(虚拟)内存地址实际上只有48宽位。

  • X86-64同时支持32位和64位可执行文件。

  • 您可以在虚拟机,而这又可以做什么,如果它想(相对于基础物理机)内运行。

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

  1. What does the size of a pointer depend on?
  2. What feature of architecture affects the size of a pointer? (In detail)
  3. How does the compiler affect the size of a pointer?

解决方案

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.

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:

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

这篇关于什么是指针的大小?究竟是依靠什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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