Peter Mawhorter
malloc
and free
From the user’s perspective:
malloc
requests some memory.
NULL
if there’s not enough memory
available.free
gives back allocated memory.
malloc
gave
you.malloc
and free
From the system’s perspective:
malloc
requests some memory.
free
gives back allocated memory.
malloc
callmovaps
require alignment:
malloc
are 16-byte aligned.(unused) | ← heap origin (16-byte aligned) |
---|---|
80 | 0b10 | ← block header (off-by-8 from aligned) |
← start of block data (16-byte aligned) | |
80 | ← block footer |
0 | 0b01 | ← heap footer |
(Alignment means last 4 bits are always 0; don’t need extra room for flags)
A = malloc(5)
(unused) | ← heap origin |
---|---|
16 | 0b11 | ← block header |
← A (aligned; 8 ≥ 5 bytes) | |
64 | 0b10 | ← block header |
64 | ← block footer |
0 | 0b01 | ← heap footer |
A = malloc(5)
; B = malloc(15)
(unused) | ← heap origin |
---|---|
16 | 0b11 | ← block header |
← A | |
32 | 0b11 | ← block header |
← B (24 ≥ 15 bytes) | |
32 | 0b10 | ← block header |
32 | ← block footer |
0 | 0b01 | ← heap footer |
A = malloc(5)
; B = malloc(15); free(A);
(unused) | ← heap origin |
---|---|
16 0b10 | ← block header |
16 | |
32 | 0b01 | ← block header |
← B (24 ≥ 15 bytes) | |
32 | 0b10 | ← block header |
32 | ← block footer |
0 | 0b01 | ← heap footer |
A = malloc(5)
;
B = malloc(15); free(A); free(B);
(unused) | ← heap origin |
---|---|
16 | 0xb10 | ← block header |
16 | |
32 | 0b00 | ← block header (fragmented) |
32 | |
32 | 0b00 | ← block header |
32 | ← block footer |
0 | 0b01 | ← heap footer |
A = malloc(5)
;
B = malloc(15); free(A); free(B);
(unused) | ← heap origin |
---|---|
80/ | 0b10 | ← block header (consolidated) |
80 | ← block footer |
0 | 0b01 | ← heap footer |
We’ve only allocated 2/80 bytes, but we cannot allocate the requested 25 bytes :(
(unused) | ← heap origin |
---|---|
16 | 0b11 | ← block header |
← A | |
32 | 0b10 | ← block header |
32 | ← block footer |
16 | 0b01 | ← block header |
← C | |
16 | 0b10 | ← block header |
16 | ← block footer |
0 | 0b01 | ← heap footer |
Group yourself:
(The document does not contain full instructions; just the questions you need to answer. Read the full instructions and then answer the questions in the document.)