Codiqo

Learn C · lesson 44 of 60 · about 10 minutes

Project: Dynamic List

Build the watch list: a reusable list of strings that grows automatically, with create, add and destroy functions.

What you'll learn

  • Wrap dynamic memory in a struct
  • Write create, add and destroy functions
  • Copy strings onto the heap

The watch list

Last, the admin types the names of processes to keep an eye on, until end. A List owns everything: an array of char * that grows, and a heap copy of every name in it. list_create makes an empty list with room for 2, list_has says whether a name is already there, list_add doubles the array when it's full and stores a copy_text copy, and list_destroy frees every name, then the array, then the list itself. main never calls malloc or free: blank lines are skipped, a name already on the list gets Already watching sshd., and the report finishes by listing the names and how much room the list has.

A small library

List owns an array of string pointers:

  • list_create() allocates the list
  • list_add(list, text) copies the text onto the heap and grows the array if needed
  • list_destroy(list) frees every string, then the array, then the list

The caller never calls malloc or free directly.

The dynamic list

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char **items;
    int count;
    int capacity;
} List;

List *list_create(void) {
    List *l = malloc(sizeof *l);
    if (l == NULL) return NULL;
    l->count = 0;
    l->capacity = 2;
    l->items = malloc(l->capacity * sizeof *l->items);
    if (l->items == NULL) { free(l); return NULL; }
    return l;
}

int list_add(List *l, const char *text) {
    if (l->count == l->capacity) {
        char **bigger = realloc(l->items, l->capacity * 2 * sizeof *bigger);
        if (bigger == NULL) return 0;
        l->items = bigger;
        l->capacity *= 2;
    }
    char *copy = malloc(strlen(text) + 1);
    if (copy == NULL) return 0;
    strcpy(copy, text);
    l->items[l->count++] = copy;
    return 1;
}

void list_destroy(List *l) {
    for (int i = 0; i < l->count; i++) free(l->items[i]);
    free(l->items);
    free(l);
}

int main(void) {
    List *tasks = list_create();
    if (tasks == NULL) return 1;
    const char *todo[] = {"Feed Phoebe", "Finish lesson", "Stretch", "Commit code"};
    for (int i = 0; i < 4; i++) list_add(tasks, todo[i]);
    for (int i = 0; i < tasks->count; i++) printf("%d. %s\n", i + 1, tasks->items[i]);
    printf("capacity %d\n", tasks->capacity);
    list_destroy(tasks);
    return 0;
}

Output

1. Feed Phoebe
2. Finish lesson
3. Stretch
4. Commit code
capacity 4

Tip

strlen(text) + 1 leaves room for the '\0' terminator. Forgetting the + 1 is a classic off-by-one bug.

Freeing from the inside out

The array holds pointers to the names, so the names must be freed while the array still exists: once the array is freed, you can't read the pointers in it any more. Free the innermost blocks first, then whatever held them.

Two names, then the array

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    char **names = malloc(2 * sizeof *names);
    if (names == NULL) return 1;
    names[0] = malloc(5);
    names[1] = malloc(5);
    if (names[0] == NULL || names[1] == NULL) {
        free(names[0]);
        free(names[1]);
        free(names);
        return 1;
    }
    strcpy(names[0], "sshd");
    strcpy(names[1], "cron");
    printf("%s %s\n", names[0], names[1]);
    for (int i = 0; i < 2; i++) free(names[i]);
    free(names);
    printf("All freed\n");
    return 0;
}

Output

sshd cron
All freed

Now try it yourself

This lesson is free in Codiqo, with 8 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.