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?

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.

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.

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 -O1

.LC0:
.ascii "practice/12/memory.c\0"
.LC1:
.ascii "m != NULL\0"
array_init:
push rbx
sub rsp, 32
mov rbx, rcx
test rcx, rcx
je .L6
.L4:
mov QWORD PTR [rbx], 0
mov DWORD PTR 8[rbx], 0
Note 1: m stays in rbx instead of being read from memory.
mov DWORD PTR 12[rbx], 0
add rsp, 32
pop rbx
ret
.L6:
mov r8d, 15
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
call [QWORD PTR __imp__assert[rip]]
jmp .L4
.LC2:
.ascii "Capacity: %d -> %d\12\0"
array_add:
push rbp
push rdi
push rsi
push rbx
sub rsp, 40
mov rbx, rcx
mov edi, edx
test rcx, rcx
je .L15
.L8:
mov eax, DWORD PTR 12[rbx]
cmp DWORD PTR 8[rbx], eax
je .L16
.L9:
movsx rdx, DWORD PTR 8[rbx]
mov rax, QWORD PTR [rbx]
mov DWORD PTR [rax+rdx*4], edi
add DWORD PTR 8[rbx], 1
mov eax, 1
.L7:
add rsp, 40
pop rbx
pop rsi
pop rdi
pop rbp
ret
.L15:
mov r8d, 24
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
call [QWORD PTR __imp__assert[rip]]
jmp .L8
.L16:
lea esi, [rax+rax]
test eax, eax
mov eax, 4
cmove esi, eax
movsx rdx, esi
sal rdx, 2
mov rcx, QWORD PTR [rbx]
call realloc
mov rbp, rax
test rax, rax
je .L13
mov edx, DWORD PTR 12[rbx]
mov r8d, esi
lea rcx, .LC2[rip]
call printf
mov QWORD PTR [rbx], rbp
mov DWORD PTR 12[rbx], esi
jmp .L9
.L13:
mov eax, 0
jmp .L7
array_free:
push rbx
sub rsp, 32
mov rbx, rcx
test rcx, rcx
je .L20
.L18:
mov rcx, QWORD PTR [rbx]
call free
mov rcx, rbx
call array_init
add rsp, 32
pop rbx
ret
.L20:
mov r8d, 42
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
call [QWORD PTR __imp__assert[rip]]
jmp .L18
copy_string:
push rdi
push rsi
push rbx
sub rsp, 32
mov rsi, rcx
call strlen
lea rdi, 1[rax]
mov rcx, rdi
call malloc
mov rbx, rax
test rax, rax
je .L21
mov r8, rdi
mov rdx, rsi
mov rcx, rax
call memcpy
.L21:
mov rax, rbx
add rsp, 32
pop rbx
pop rsi
pop rdi
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 rsi
push rbx
sub rsp, 56
call __main
lea rcx, 32[rsp]
call array_init
mov ebx, 1
mov edx, 1
lea rsi, 32[rsp]
.L27:
mov rcx, rsi
call array_add
test eax, eax
je .L31
add ebx, 1
mov edx, ebx
imul edx, ebx
cmp edx, 999
jle .L27
mov r8d, DWORD PTR 44[rsp]
mov edx, DWORD PTR 40[rsp]
lea rcx, .LC4[rip]
call printf
movsx rdx, DWORD PTR 40[rsp]
mov rax, QWORD PTR 32[rsp]
mov edx, DWORD PTR -4[rax+rdx*4]
lea rcx, .LC5[rip]
call printf
lea rcx, 32[rsp]
call array_free
lea rcx, .LC6[rip]
call copy_string
mov rbx, rax
test rax, rax
je .L32
mov rcx, rax
call strlen
lea r8, 1[rax]
mov rdx, rbx
lea rcx, .LC7[rip]
call printf
mov rcx, rbx
call free
mov eax, 0
jmp .L24
.L31:
lea rcx, .LC3[rip]
call printf
lea rcx, 32[rsp]
call array_free
mov eax, 1
.L24:
add rsp, 56
pop rbx
pop rsi
ret
.L32:
lea rcx, .LC3[rip]
call printf
mov eax, 1
jmp .L24