Skip to the task
Layout
Back to classic view

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

RUNTask 9 of 20 · step 4: An array that growsrealloc in the debugger
realloc in the debugger in practice/12/

A breakpoint at m->data = new_data;, Debug. At each stop compare m->data and new_data.

Checklist

  • Every task is one commit with a clear message.
  • Zero compiler warnings: the code builds with -Werror.
  • Once per topic you step through your code in the debugger (CLion, gdb).
  • Every function has preconditions and postconditions, and assert tests check them.
  • A multi-file program: .h files with Doxygen comments and their own CMake target.
  • Input only through the safe functions of your own input.h.
  • Every error code is checked (fopen, fclose, malloc and so on).
  • CI is clean: no AddressSanitizer or UBSan reports.

Numbered notes in the code explain the important lines.

Details & hint

You should see: Sometimes the address is the same (the block grew in place), sometimes different (realloc moved the data).

practice 12 memory.c Code Diff +53 −15
M · MODIFIED git diff: red = old code, green = new.
--- a/practice/12/memory.c
+++ b/practice/12/memory.c
@@ -1,7 +1,49 @@
+ #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) {
@@ -15,22 +57,18 @@
}
int main(void) {
− size_t n = 10;
− int *squares = malloc(n * sizeof *squares);
− if (squares == NULL) {
− printf("Out of memory.\n");
− return EXIT_FAILURE;
− }
− for (size_t i = 0; i < n; i++) {
− squares[i] = (int)((i + 1) * (i + 1));
− }
− printf("Squares:");
− for (size_t i = 0; i < n; i++) {
− printf(" %d", squares[i]);
+ 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("\n");
− free(squares);
− squares = NULL;
+ 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) {
opened for this task
$ .\build\debug\practice\12\p12_memory.exe
Capacity: 0 -> 4
Capacity: 4 -> 8
Capacity: 8 -> 16
Capacity: 16 -> 32
Squares below 1000: 31, capacity 32
Last: 961
Copy: Ona Petraite (13 bytes)