Memory Layout
#lecture note based on 15-213 Introduction to Computer Systems
H2 Overall Memory Layout (for a process)
A process’s virtual memory, assuming it’s running one thread.
H2 Bytes in Memory
H3 Integers & byte ordering
- int as memory address always writes byte location of first byte of whatever thing, regardless the size of the thing. (L3S46)
- byte order
- Big Endian (Sun, network packet headers, etc.): least signifiant byte highest address
- Little Endian (x86 (including those embedded in x86 machine code), iOS, etc.): least significant byte lowest address
- Bi Endian (e.g. ARM): can be configured either way
Byte ordering example. say x = 0x12345678
and &x = 0x100
Big Endian
0x100 0x101 0x102 0x103
12 34 56 78
Little Endian
0x100 0x101 0x102 0x103
78 56 34 12
Why little endian makes sense - truncation doesn’t change address.
short foo (int x) {
// return (short) x; can be rewritten as:
return *(short*)&x;
// not true if big endian
}
H3 String
No byte ordering issue. Just go down the address (make sure to null terminate)