Skip to the task
Layout
Back to classic view
READTask 1 of 17 · step 1: How the test worksHow the test works
How the test works in practice/12/

Two parts: questions without a compiler and two programs in the test/2 folder with autotests. They give no points.

Details & hint

The test after topic 12 checks topics 7–12: modules, pointers and bits, strings, structs, files and dynamic memory. This lesson is practice in the same shape.

Two parts

  • Questions. Short code: what it prints, why it doesn't compile, where the bug is. You answer on your own, without a compiler. There are twelve here, each with an answer and an explanation.
  • Programs. Two small tasks in the test/2/ folder: words.c and shopping.c. Autotests check them, like the homework.

How long the real test takes and what you may use will be announced by your lecturer. Practice has no time limit, but try to write both tasks within an hour and a half.

How to check the programs

The task descriptions and the exact output text are in steps 4 and 5 and in the test/2/tests/ files. Run the tests like this:

ctest --preset debug -L test2

In CLion: the All CTest configuration and ▶. Both files already have the #includes and main; while they are empty, the tests fail.

These tasks give no points: GitHub Actions builds them and runs them with ASan and UBSan, but "Points" doesn't count them. They are for checking yourself.

What is checked, same as in the homework

  • the program builds without warnings (-Wall -Wextra -Werror -pedantic -Wvla);
  • the output matches the expected text letter by letter;
  • each part is a function with a contract and an assert, and main only calls them;
  • shopping.c: realloc, free and fclose are there, fscanf isn't, and GitHub CI finds no leak;
  • no TODO is left.

If you get stuck, go back to the topic the question comes from: every answer says which topic it belongs to.

practice 12 memory.c Code Diff
UNCHANGED This step does not change this file.
#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;
}