指针如何存储在内存中? [英] How is a pointer stored in memory?
问题描述
我正在研究OS项目,我只是想知道指针如何存储在内存中?我知道一个指针是4个字节,那么该指针如何在这4个字节之间分布?
I am working on an OS project and I am just wondering how a pointer is stored in memory? I understand that a pointer is 4 bytes, so how is the pointer spread amongst the 4 bytes?
我的问题是,我正在尝试存储一个指向4字节内存插槽的指针.可以说指针是0x7FFFFFFF. 4个字节中的每个字节存储什么?
My issue is, I am trying to store a pointer to a 4 byte slot of memory. Lets say the pointer is 0x7FFFFFFF. What is stored at each of the 4 bytes?
推荐答案
指针的存储方式与任何其他多字节值相同.这4个字节根据系统的字节序进行存储.假设这4个字节的地址如下:
The way that pointer is stored is same as any other multi-byte values. The 4 bytes are stored according to the endianness of the system. Let's say the address of the 4 bytes is below:
大端(最高有效字节在前):
Big endian (most significant byte first):
Address Byte
0x1000 0x7F
0x1001 0xFF
0x1002 0xFF
0x1003 0xFF
小字节序(最低有效字节在前):
Small endian (least significant byte first):
Address Byte
0x1000 0xFF
0x1001 0xFF
0x1002 0xFF
0x1003 0x7F
顺便说一句,4字节地址是32位系统. 64位系统具有8个字节的地址.
Btw, 4 byte address is 32-bit system. 64-bit system has 8 bytes addresses.
要引用指针的每个单独部分,您需要使用指针. :) 说你有:
To reference each individual part of the pointer, you need to use pointer. :) Say you have:
int i = 0;
int *pi = &i; // say pi == 0x7fffffff
int **ppi = π // from the above example, int ppi == 0x1000
简单的指针算法将使您获得指向每个字节的指针.
Simple pointer arithmetic would get you the pointer to each byte.
这篇关于指针如何存储在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!