指针占用多少字节? [英] How many bytes do pointers take up?

查看:185
本文介绍了指针占用多少字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对指针及其占用多少字节感到有些困惑.在我的教科书中,它首先说在16位系统上的指针占用2个字节,在32位系统上占用4个字节,在64位系统上占用8个字节,依此类推.然后过了10行,它说指针占用了保存地址所需的字节数.这是我的问题:

I am little bit confused about pointers and how many bytes they take up. In my textbook it first says that pointers on 16 bit systems take up 2 bytes, 32 bit systems 4 bytes, 64 bit system 8 bytes and so on. Then 10 lines after, it says that pointers take up that many bytes, that are needed to hold the addresses. Here are my questions :

  1. 那么这是否意味着如果让我们说在64位系统上,地址最多需要8个字节?
  2. 如果我们使用16位系统,并且指针占用2个字节,而地址需要再放置2个字节,那么会发生什么呢?

推荐答案

没有固定答案.它完全取决于体系结构,编译器实现,甚至取决于指针本身的类型.不能保证指向不同类型的指针具有相同的大小和/或表示形式.

There is no fixed answer; it depends entirely on the architecture, the compiler implementation, and even the type of the pointer itself. Pointers to different types are not guaranteed to have the same size and/or representation.

例如,假设一个字寻址架构,其中最小的可寻址存储单元为16位宽(或更宽).每个字可以包含多个char值;所有其他类型占一个完整的单词或更多.在这样的体系结构上,与其他指针类型相比,char *void *将需要一些额外的位才能偏移到字中.

For example, assume a word-addressed architecture, where the smallest addressable unit of storage is 16 bits wide (or wider). Each word can hold multiple char values; all other types take up a full word or more. On such an architecture, a char * and void * would need some extra bits to offset into the word compared to other pointer types.

还请注意,指针类型可能比存储地址实际所需的位数.原始的Macintosh运行在Motorola 68000 CPU上,该CPU的字长为32位,但地址总线上只有24位.指针类型为32位宽,高8位未使用.进取的MacOS程序员利用这一点将数据存储到指针类型的最高字节,从而充分利用了宝贵的128 KB RAM.当然,摩托罗拉最终发布了带有32条地址线的CPU(68020),这意味着所有这些代码都必须重写.

Note also that a pointer type may be wider than the number of bits actually required to store an address. The original Macintosh ran on a Motorola 68000 CPU, which had a 32-bit word size, but only 24 bits on the address bus. Pointer types were 32 bits wide, leaving the upper 8 bits unused. Enterprising MacOS programmers took advantage of that to store some data to the uppermost byte of a pointer type, making the most of that precious 128 KB of RAM. Of course, Motorola eventually released a CPU with 32 address lines (the 68020), meaning all that code had to be rewritten.

在现代的商用台式机和服务器硬件(阅读:x86)上,可以合理地假设所有指针类型的大小与本机字大小(32位或64位)相同,并且所有指针类型都具有相同的大小和表示形式.请注意,这没有是真的.

On modern, commodity desktop and server hardware (read: x86), it's reasonably safe to assume that all pointer types are the same size as the native word size (32- or 64-bit), and that all pointer types have the same size and representation. Just be aware that this doesn't have to be true.

这篇关于指针占用多少字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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