Skip to the task
Layout
Back to classic view

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

CHECKTask 18 of 20 · step 7: Extra: how does the CPU see it?What does realloc get?
What does realloc get? in practice/06/

Find call realloc in array_add. What goes into rcx and rdx before the call, and where does the result end up?

Numbered notes explain the assembly lines.

Details & hint

Stuck? rcx: the old address m->data, rdx: the new size in bytes, new_capacity · 4. The new address comes back in rax, is stored in new_data and is then compared with 0 (NULL).

practice 06 hall.c Code Diff
UNCHANGED This step does not change this file.
#include <assert.h>
#include <stdio.h>
#define ROWS 4
#define SEATS 6
/* How many free seats (0) there are in a row.
Before: 0 <= row < ROWS.
After: a number from 0 to SEATS. */
int free_seats(int hall[][SEATS], int row) {
assert(row >= 0 && row < ROWS);
int count = 0;
for (int s = 0; s < SEATS; s++) {
if (hall[row][s] == 0) {
count++;
}
}
return count;
}
/* The first row that has k free seats side by side.
Before: 1 <= k <= SEATS.
After: the row's index, or -1 if there is none. */
int find_row(int hall[][SEATS], int k) {
assert(k >= 1 && k <= SEATS);
for (int r = 0; r < ROWS; r++) {
int in_a_row = 0;
for (int s = 0; s < SEATS; s++) {
if (hall[r][s] == 0) {
in_a_row++;
if (in_a_row == k) {
return r;
}
} else {
in_a_row = 0;
}
}
}
return -1;
}
int main(void) {
int hall[ROWS][SEATS] = {
{1, 1, 0, 0, 1, 1},
{0, 1, 1, 1, 1, 0},
{0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 1, 0},
};
for (int r = 0; r < ROWS; r++) {
printf("Row %d: ", r + 1);
for (int s = 0; s < SEATS; s++) {
if (hall[r][s] == 1) {
printf("#");
} else {
printf(".");
}
}
printf(" free: %d\n", free_seats(hall, r));
}
for (int k = 2; k <= 5; k++) {
int r = find_row(hall, k);
if (r == -1) {
printf("No %d seats side by side.\n", k);
} else {
printf("%d seats side by side: row %d.\n", k, r + 1);
}
}
return 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