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 main.c Code Diff
UNCHANGED This step does not change this file.
#include <assert.h>
#include <stdio.h>
#include "input.h"
#define DAYS 7
/* Reads n temperatures into the array t.
Before: 1 <= n <= DAYS.
After: t[0]..t[n - 1] are filled. */
void read_temperatures(int t[], int n) {
assert(n >= 1 && n <= DAYS);
for (int i = 0; i < n; i++) {
printf("%d. ", i + 1);
t[i] = read_int("Temperature: ");
}
}
/* Prints n values of the array on one line, separated by a space.
Before: 0 <= n <= DAYS. */
void print_array(const int t[], int n) {
assert(n >= 0 && n <= DAYS);
for (int i = 0; i < n; i++) {
if (i > 0) {
printf(" ");
}
printf("%d", t[i]);
}
printf("\n");
}
/* The arithmetic mean.
Before: 1 <= n <= DAYS.
After: (t[0] + ... + t[n - 1]) / n. */
double average(const int t[], int n) {
assert(n >= 1 && n <= DAYS);
int sum = 0;
for (int i = 0; i < n; i++) {
sum += t[i];
}
return (double)sum / n;
}
/* The index of the warmest day (the first one, if several are equal).
Before: 1 <= n <= DAYS.
After: t[i] >= t[j] for every j. */
int warmest(const int t[], int n) {
assert(n >= 1 && n <= DAYS);
int best = 0;
for (int i = 1; i < n; i++) {
if (t[i] > t[best]) {
best = i;
}
}
return best;
}
/* Linear search: the index of the first such value.
Before: 0 <= n <= DAYS.
After: i such that t[i] == value, or -1 if there is none. */
int find(const int t[], int n, int value) {
assert(n >= 0 && n <= DAYS);
for (int i = 0; i < n; i++) {
if (t[i] == value) {
return i;
}
}
return -1;
}
/* Sorts in ascending order, by selection.
Before: 0 <= n <= DAYS.
After: t[0] <= t[1] <= ... <= t[n - 1]. */
void sort_array(int t[], int n) {
assert(n >= 0 && n <= DAYS);
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (t[j] < t[min]) {
min = j;
}
}
int temp = t[i];
t[i] = t[min];
t[min] = temp;
}
}
int main(void) {
int temperatures[DAYS];
read_temperatures(temperatures, DAYS);
printf("Temperatures: ");
print_array(temperatures, DAYS);
printf("Average: %.2f\n", average(temperatures, DAYS));
int w = warmest(temperatures, DAYS);
printf("Warmest: day %d, %d C\n", w + 1, temperatures[w]);
int sorted[DAYS];
for (int i = 0; i < DAYS; i++) {
sorted[i] = temperatures[i];
}
sort_array(sorted, DAYS);
printf("Sorted: ");
print_array(sorted, DAYS);
printf("Median: %d C\n", sorted[DAYS / 2]);
int target = read_int("Which temperature to look for? ");
int pos = find(temperatures, DAYS, target);
if (pos == -1) {
printf("%d C was not on any day.\n", target);
} else {
printf("%d C was first on day %d.\n", target, pos + 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