Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a pointer stored in memory?

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?

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?

like image 919
Tesla Avatar asked Dec 27 '22 22:12

Tesla


1 Answers

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

Btw, 4 byte address is 32-bit system. 64-bit system has 8 bytes addresses.

EDIT: 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.

like image 198
yuklai Avatar answered Feb 19 '23 17:02

yuklai