Skip to the task
Layout
Back to classic view

You are looking ahead. Your place is still task 1: malloc.

READTask 16 of 20 · step 7: Extra: how does the CPU see it?How does the CPU see it?
How does the CPU see it? in practice/11/

Extra: to the processor, a struct's fields are offsets from its start, and malloc, realloc and free are plain calls. Look at practice/12/memory.c.

Numbered notes explain the assembly lines.

Details & hint

Extra. Not graded. What do a struct, malloc and free look like to the processor?

Open the tool window's Assembly tab. On the left is your practice/12/memory.c.

A struct's fields are offsets

In memory, an IntArray is 16 bytes: data (a pointer, 8 bytes), then count and capacity (4 each). The processor doesn't know a field's name, only its offset from the start of the struct:

  • m->data is [rax] (offset 0);
  • m->count is 8[rax];
  • m->capacity is 12[rax].

m->count is two steps, as in topic 8: first the pointer m is taken, then the value 8 bytes further on is read.

What to notice (-O0)

  • realloc, malloc and free are plain calls, like your own functions. realloc gets the old address in rcx and the size in bytes in rdx (new_capacity · 4, lea rdx, 0[0+rax*4]), and returns the new address in rax.
  • if (new_data == NULL) is a comparison with 0 right after the call.
  • The heap itself is not in the assembly, only calls. What is allocated, how much and when is known only while the program runs, so the compiler can't see a leak and AddressSanitizer can.

Switch to -O1: array_init shrinks to a few writes, and m stays in a register instead of being read from memory for every field.

practice 11 bad.txt Code Diff
UNCHANGED This step does not change this file.
20.5
21,5
22.0
opened for this task

Extra · x86-64 · Intel syntax · Windows (MinGW GCC)

-O0 -O1

C source memory.c

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* A resizable array of integers. */
typedef struct {
int *data; /* the elements in dynamic memory, or NULL */
int count; /* how many elements are stored */
int capacity; /* how many fit before it has to grow */
} IntArray;
/* An empty array: no memory yet. Before: m != NULL. */
void array_init(IntArray *m) {
assert(m != NULL);
m->data = NULL;
m->count = 0;
m->capacity = 0;
}
/* Adds x at the end; when there is no room, doubles the capacity.
Before: m != NULL. After: 1 - added, 0 - out of memory (m unchanged). */
int array_add(IntArray *m, int x) {
assert(m != NULL);
if (m->count == m->capacity) {
int new_capacity = m->capacity == 0 ? 4 : m->capacity * 2;
int *new_data = realloc(m->data, (size_t)new_capacity * sizeof *new_data);
if (new_data == NULL) {
return 0;
}
printf("Capacity: %d -> %d\n", m->capacity, new_capacity);
m->data = new_data;
m->capacity = new_capacity;
}
m->data[m->count] = x;
m->count++;
return 1;
}
/* Gives the memory back and leaves an empty array. Before: m != NULL. */
void array_free(IntArray *m) {
assert(m != NULL);
free(m->data);
array_init(m);
}
/* Returns a new copy of s in dynamic memory, or NULL if there isn't enough.
Before: s != NULL. After: the caller frees the copy with free. */
char *copy_string(const char *s) {
size_t size = strlen(s) + 1;
char *copy = malloc(size);
if (copy == NULL) {
return NULL;
}
memcpy(copy, s, size);
return copy;
}
int main(void) {
IntArray squares;
array_init(&squares);
for (int i = 1; i * i < 1000; i++) {
if (!array_add(&squares, i * i)) {
printf("Out of memory.\n");
array_free(&squares);
return EXIT_FAILURE;
}
}
printf("Squares below 1000: %d, capacity %d\n", squares.count, squares.capacity);
printf("Last: %d\n", squares.data[squares.count - 1]);
array_free(&squares);
char *name = copy_string("Ona Petraite");
if (name == NULL) {
printf("Out of memory.\n");
return EXIT_FAILURE;
}
printf("Copy: %s (%zu bytes)\n", name, strlen(name) + 1);
free(name);
return EXIT_SUCCESS;
}

x86-64 assembly -O0

.LC0:
.ascii "practice/12/memory.c\0"
.LC1:
.ascii "m != NULL\0"
array_init:
push rbp
mov rbp, rsp
sub rsp, 32
mov QWORD PTR 16[rbp], rcx
cmp QWORD PTR 16[rbp], 0
jne .L6
mov r8d, 15
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
.L6:
mov rax, QWORD PTR 16[rbp]
mov QWORD PTR [rax], 0
Note 1: m->data: offset 0.
mov rax, QWORD PTR 16[rbp]
mov DWORD PTR 8[rax], 0
Note 2: m->count: offset 8, after the 8-byte pointer.
mov rax, QWORD PTR 16[rbp]
mov DWORD PTR 12[rax], 0
Note 3: m->capacity: offset 12.
nop
leave
ret
.LC2:
.ascii "Capacity: %d -> %d\12\0"
array_add:
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR 16[rbp], rcx
mov DWORD PTR 24[rbp], edx
cmp QWORD PTR 16[rbp], 0
jne .L10
mov r8d, 24
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
.L10:
mov rax, QWORD PTR 16[rbp]
mov edx, DWORD PTR 8[rax]
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR 12[rax]
cmp edx, eax
jne .L11
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR 12[rax]
test eax, eax
je .L12
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR 12[rax]
add eax, eax
jmp .L13
.L12:
mov eax, 4
.L13:
mov DWORD PTR -4[rbp], eax
mov eax, DWORD PTR -4[rbp]
cdqe
lea rdx, 0[0+rax*4]
mov rax, QWORD PTR 16[rbp]
mov rax, QWORD PTR [rax]
mov rcx, rax
call realloc
Note 4: rcx: the old address, rdx: bytes; the new address comes back in rax.
mov QWORD PTR -16[rbp], rax
cmp QWORD PTR -16[rbp], 0
Note 5: new_data == NULL is a comparison with 0.
jne .L14
mov eax, 0
jmp .L15
.L14:
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR 12[rax]
mov edx, DWORD PTR -4[rbp]
mov r8d, edx
mov edx, eax
lea rcx, .LC2[rip]
call printf
mov rax, QWORD PTR 16[rbp]
mov rdx, QWORD PTR -16[rbp]
mov QWORD PTR [rax], rdx
mov rax, QWORD PTR 16[rbp]
mov edx, DWORD PTR -4[rbp]
mov DWORD PTR 12[rax], edx
.L11:
mov rax, QWORD PTR 16[rbp]
mov rdx, QWORD PTR [rax]
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR 8[rax]
cdqe
sal rax, 2
add rdx, rax
mov eax, DWORD PTR 24[rbp]
mov DWORD PTR [rdx], eax
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR 8[rax]
lea edx, 1[rax]
mov rax, QWORD PTR 16[rbp]
mov DWORD PTR 8[rax], edx
mov eax, 1
.L15:
leave
ret
array_free:
push rbp
mov rbp, rsp
sub rsp, 32
mov QWORD PTR 16[rbp], rcx
cmp QWORD PTR 16[rbp], 0
jne .L19
mov r8d, 42
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
.L19:
mov rax, QWORD PTR 16[rbp]
mov rax, QWORD PTR [rax]
mov rcx, rax
call free
mov rcx, QWORD PTR 16[rbp]
call array_init
nop
leave
ret
copy_string:
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR 16[rbp], rcx
mov rcx, QWORD PTR 16[rbp]
call strlen
add rax, 1
mov QWORD PTR -8[rbp], rax
mov rax, QWORD PTR -8[rbp]
mov rcx, rax
call malloc
mov QWORD PTR -16[rbp], rax
cmp QWORD PTR -16[rbp], 0
jne .L21
mov eax, 0
jmp .L22
.L21:
mov rdx, QWORD PTR -8[rbp]
mov rax, QWORD PTR -16[rbp]
mov r8, rdx
mov rdx, QWORD PTR 16[rbp]
mov rcx, rax
call memcpy
mov rax, QWORD PTR -16[rbp]
.L22:
leave
ret
.LC3:
.ascii "Out of memory.\12\0"
.LC4:
.ascii "Squares below 1000: %d, capacity %d\12\0"
.LC5:
.ascii "Last: %d\12\0"
.LC6:
.ascii "Ona Petraite\0"
.LC7:
.ascii "Copy: %s (%zu bytes)\12\0"
main:
push rbp
mov rbp, rsp
sub rsp, 64
call __main
lea rax, -32[rbp]
mov rcx, rax
call array_init
mov DWORD PTR -4[rbp], 1
jmp .L24
.L27:
mov eax, DWORD PTR -4[rbp]
imul eax, eax
mov edx, eax
lea rax, -32[rbp]
mov rcx, rax
call array_add
test eax, eax
jne .L25
lea rcx, .LC3[rip]
call printf
lea rax, -32[rbp]
mov rcx, rax
call array_free
mov eax, 1
jmp .L29
.L25:
add DWORD PTR -4[rbp], 1
.L24:
mov eax, DWORD PTR -4[rbp]
imul eax, eax
cmp eax, 999
jle .L27
mov edx, DWORD PTR -20[rbp]
mov eax, DWORD PTR -24[rbp]
mov r8d, edx
mov edx, eax
lea rcx, .LC4[rip]
call printf
mov rdx, QWORD PTR -32[rbp]
mov eax, DWORD PTR -24[rbp]
cdqe
sal rax, 2
sub rax, 4
add rax, rdx
mov eax, DWORD PTR [rax]
mov edx, eax
lea rcx, .LC5[rip]
call printf
lea rax, -32[rbp]
mov rcx, rax
call array_free
lea rcx, .LC6[rip]
call copy_string
mov QWORD PTR -16[rbp], rax
cmp QWORD PTR -16[rbp], 0
jne .L28
lea rcx, .LC3[rip]
call printf
mov eax, 1
jmp .L29
.L28:
mov rax, QWORD PTR -16[rbp]
mov rcx, rax
call strlen
lea rdx, 1[rax]
mov rax, QWORD PTR -16[rbp]
mov r8, rdx
mov rdx, rax
lea rcx, .LC7[rip]
call printf
mov rax, QWORD PTR -16[rbp]
mov rcx, rax
call free
mov eax, 0
.L29:
leave
ret