Skip to main content
Be Bitwise
CWhat Your C Code Actually Does

Memory Operations in Assembly

You've leaned on malloc, free, pointer arithmetic, arrays, and structs throughout this track. Time to peek at what those operations actually become down at the metal. Every pointer dereference, every array index, every struct field access ends up as a specific x86-64 addressing mode.

How malloc and free Work at the Syscall Level

malloc(size) lands in a C library function, not a syscall. The libc allocator — glibc's ptmalloc, musl's malloc, whatever your distro ships — manages its own pool and only knocks on the kernel's door when that pool runs dry.

Underneath, the allocator leans on two syscalls:

  • brk/sbrk - Moves the "program break," expanding the heap contiguously. Used for small allocations.
  • mmap - Maps a new region of virtual memory. Used for large allocations (typically > 128KB).

A malloc call in assembly looks like this:

c
int *p = malloc(sizeof(int));
asm
mov edi, 4 ; sizeof(int) = 4 bytes
call malloc ; Returns pointer in rax
mov QWORD PTR [rbp-8], rax ; Store the pointer on the stack

Internally malloc is a beast. A small request that glibc can satisfy from its per-thread cache (the tcache) takes a few dozen instructions, but the slow paths juggle free lists, chunk headers and arena locks, and can run to hundreds or end in a system call. From your code's vantage point though, it's three steps: put the size in edi, call, collect a pointer in rax.

When the allocator does need to ask the kernel for more, you'd see something like:

asm
; Inside malloc's implementation, requesting memory via brk
mov eax, 12 ; syscall number for brk
xor edi, edi ; argument: 0 means "get current break"
syscall ; Returns current break in rax
; ... later ...
mov eax, 12 ; brk again
lea rdi, [rax+4096] ; Request 4096 more bytes
syscall ; Kernel extends the heap
Syscall Mechanics

On x86-64 Linux, syscalls use a specific convention: the syscall number goes in rax, arguments in rdi, rsi, rdx, r10, r8, r9 (note: r10 instead of rcx because syscall clobbers rcx). The syscall instruction transitions from user mode to kernel mode.

Pointer Arithmetic in Assembly

Pointer arithmetic is one of C's sharpest tools. Down at assembly level, it boils away to plain address math.

c
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;
int val = *(p + 3); // Access arr[3]
asm
; Assume arr starts at [rbp-20] (5 ints * 4 bytes = 20 bytes)
lea rax, [rbp-20] ; rax = address of arr[0]
mov QWORD PTR [rbp-28], rax ; p = arr
mov rax, QWORD PTR [rbp-28] ; Load p
add rax, 12 ; p + 3 means +12 bytes (3 * sizeof(int))
mov eax, DWORD PTR [rax] ; Dereference: load the int at that address
mov DWORD PTR [rbp-32], eax ; Store in val

The thing worth noticing: p + 3 in C lands as p + 12 in assembly. The compiler silently multiplies your offset by the size of whatever the pointer points at. That's the whole game with pointer arithmetic — automatic scaling by sizeof(int).

Array Indexing as Base + Offset

arr[i] in C is syntactic sugar for *(arr + i). x86-64 has a whole addressing mode dedicated to exactly this shape:

c
int arr[10];
int x = arr[3];
int y = arr[i]; // i is a variable
asm
; Fixed index: arr[3]
mov eax, DWORD PTR [rbp-40+12] ; base + 3*4 = base + 12
; Variable index: arr[i]
mov eax, DWORD PTR [rbp-44] ; Load i
cdqe ; Sign-extend eax to rax
mov eax, DWORD PTR [rbp-40+rax*4] ; base + i*4

The [rbp-40+rax*4] form is scaled index addressing. The CPU computes base + index * scale in one instruction, where scale picks from 1, 2, 4, or 8. The mode is custom-built for array access — the silicon was designed with C-style arrays already in mind.

Struct Field Access as Fixed Offsets

Struct field access compiles down to plain offset math. The compiler nails down each field's byte offset before your program even runs.

c
struct Point {
int x; // offset 0
int y; // offset 4
double z; // offset 8 (aligned to 8 bytes)
};
void process(struct Point *p) {
p->x = 10;
p->y = 20;
p->z = 3.14;
}
asm
process:
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-8], rdi ; Store pointer p on stack
mov rax, QWORD PTR [rbp-8] ; Load p
mov DWORD PTR [rax], 10 ; p->x: offset 0 from p
mov rax, QWORD PTR [rbp-8] ; Reload p
mov DWORD PTR [rax+4], 20 ; p->y: offset 4 from p
mov rax, QWORD PTR [rbp-8] ; Reload p
movsd xmm0, QWORD PTR .LC0 ; Load 3.14 into SSE register
movsd QWORD PTR [rax+8], xmm0 ; p->z: offset 8 from p

Notice:

  • p->x is simply [rax] - offset 0
  • p->y is [rax+4] - offset 4 (right after the 4-byte int)
  • p->z is [rax+8] - offset 8 (aligned to 8 bytes for the double)

Hover any field below. Its declaration, the line that writes it and the store that hits its offset all light up in one colour, so you can match int y; // offset 4 straight to [rax+4].

struct Point fields as fixed offsets from p

x86-64
C Source
1struct Point {
2 int x; // offset 0
3 int y; // offset 4
4 double z; // offset 8
5};
6
7void process(struct Point *p) {
8 p->x = 10;
9 p->y = 20;
10 p->z = 3.14;
11}
Assembly
1process:
2 push rbp
3 mov rbp, rsp
4 mov QWORD PTR [rbp-8], rdi
5 mov rax, QWORD PTR [rbp-8]
6 mov DWORD PTR [rax], 10
7 mov rax, QWORD PTR [rbp-8]
8 mov DWORD PTR [rax+4], 20
9 mov rax, QWORD PTR [rbp-8]
10 movsd xmm0, QWORD PTR .LC0
11 movsd QWORD PTR [rax+8], xmm0

Hover over code lines or mapping badges to see correspondences

The -> operator in C compiles down to one thing: adding a constant to a pointer. Struct field access carries zero runtime cost. It's all baked in by the compiler.

Struct Padding and Alignment

The compiler quietly slips padding bytes in to keep fields properly aligned:

c
struct Padded {
char a; // offset 0, size 1
// 3 bytes padding
int b; // offset 4, size 4
char c; // offset 8, size 1
// 7 bytes padding
double d; // offset 16, size 8
};
// Total size: 24 bytes (not 14!)

You can confirm it in the assembly by reading off the offsets:

asm
mov BYTE PTR [rax], 65 ; a at offset 0
mov DWORD PTR [rax+4], 42 ; b at offset 4 (not 1!)
mov BYTE PTR [rax+8], 66 ; c at offset 8
movsd QWORD PTR [rax+16], xmm0 ; d at offset 16 (not 9!)

Aligned data lets the CPU pull a value in one operation. Misaligned reads — say, grabbing a 4-byte int from an address not divisible by 4 — either pay for two memory reads or, on some architectures, fault outright. Padding structs is how the compiler dodges that pothole.

The memcpy and memset Patterns

Assign one struct to another and the compiler emits a copy:

c
struct Point a = {1, 2, 3.14};
struct Point b = a; // Copy entire struct

Small structs get copied field-by-field with mov instructions. Larger ones tend to fall back on rep movsb or a flat-out call to memcpy:

asm
; Small struct copy (inline)
mov rax, QWORD PTR [rbp-24] ; Copy first 8 bytes
mov QWORD PTR [rbp-48], rax
mov rax, QWORD PTR [rbp-16] ; Copy next 8 bytes
mov QWORD PTR [rbp-40], rax
; Large struct copy (rep movsb)
lea rdi, [rbp-200] ; Destination
lea rsi, [rbp-400] ; Source
mov ecx, 200 ; Byte count
rep movsb ; Copy ecx bytes from [rsi] to [rdi]

Stack vs Heap: What the CPU Sees

From the CPU's perspective stack and heap are indistinguishable. Both are just addresses in virtual memory. The difference lives entirely in how they're managed:

c
// Stack allocation
int stack_val = 42;
// Assembly: mov DWORD PTR [rbp-4], 42
// One instruction. Instant.
// Heap allocation
int *heap_val = malloc(sizeof(int));
*heap_val = 42;
// Assembly: call malloc; mov DWORD PTR [rax], 42
// Dozens of instructions inside malloc on the fast path, many more on a slow one.

Stack allocation is one instruction. Often it's nothing at all — the compiler nudges rsp once on entry to cover every local variable in the function. Heap allocation, by contrast, drags you into the allocator: free-list searches, possibly a syscall, metadata bookkeeping. The performance gap between the two isn't subtle.

Practical: Examining Your Own Code

Compile any C file that uses structs and pointers, then poke around the output:

bash
gcc -S -masm=intel -O0 your_file.c -o your_file.s

Look for patterns:

  • DWORD PTR [rax+N] - struct field access at offset N
  • [rbp-N+rax*4] - array indexing with a variable index
  • call malloc followed by stores through rax - heap allocation and initialisation
  • rep movsb or sequences of mov - struct or array copies

Every memory operation in your C code lands on one of those patterns. No garbage collector running quietly in the background, no runtime checks waiting to bite. The instructions you read in the assembly are the ones the CPU executes, though not necessarily how it executes them: the next lesson shows the CPU splitting them into micro-ops, running them out of order and guessing ahead at branches, and that's where the remaining hidden costs live.