Skip to the task
Layout
Back to classic view

You are looking at lesson 12. Your place is still lesson 5, task 1: Functions.

EDITTask 7 of 20 · step 4: An array that growsAn array that grows
An array that grows in practice/12/

IntArray with data, count and capacity; array_init, array_add with realloc and doubling, array_free; at the start of main – squares below 1000.

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
  • int *new_data = realloc(...) – into a temporary pointer: on NULL, the old block stays in m->data.
  • The fixed malloc leaves main: now the number of squares isn't known in advance. copy_string stays the same.
practice 12 memory.c Code Diff +53 −15
M · MODIFIED Green = add, blue = change, red mark = removed line.
#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 */
Note 1: count ≤ capacity: how many are written and how many fit.
} 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;
Note 2: Doubling: 1000 elements take only 9 reallocs.
int *new_data = realloc(m->data, (size_t)new_capacity * sizeof *new_data);
Note 3: Into a temporary pointer, not straight into m->data.
if (new_data == NULL) {
return 0;
Note 4: m is unchanged: the old block is still valid.
}
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);
Note 5: free(NULL) does nothing – freeing an empty array is fine.
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);
Note 6: On the error path and at the end too.
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;
}