Skip to the task
Layout
Back to classic view

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

READTask 10 of 20 · step 5: Common mistakesCommon mistakes
Common mistakes in practice/10/

An unchecked malloc, p = realloc(p, ...), malloc(n) without sizeof, a forgotten '\0', a leak, use after free, two frees, old pointers after realloc, sizeof p.

Details & hint

Memory errors are most dangerous because under MinGW the program often "works". GitHub CI with ASan catches them.

Common mistakes
  • An unchecked malloc/realloc. On NULL, p[0] = 1 writes into nowhere. After every one – if (p == NULL).
  • p = realloc(p, ...). On NULL the old block is lost and can never be freed. Into a temporary pointer first.
  • malloc(n) instead of malloc(n * sizeof *p). 10 int is 40 bytes, not 10. ASan: heap-buffer-overflow.
  • A forgotten '\0'. A copy of a string needs strlen(s) + 1 bytes.
  • Memory never freed. LeakSanitizer: detected memory leaks. Especially on an early return after an error: group_free is needed there too.
  • Use after free. The pointer still holds the address, but the block isn't yours. ASan: heap-use-after-free; when the free and the use are in the same function, CI's GCC already stops at compile time: pointer ‘p’ used after ‘free’. After free – p = NULL.
  • free twice. ASan: attempting double-free (when the two free calls are in different functions; in the same function CI's GCC refuses to compile it: used after ‘free’). p = NULL after free protects here too: free(NULL) does nothing.
  • free on something malloc didn't give. free(&x) or free(p + 1) – undefined behaviour.
  • Old pointers after realloc. int *first = &m.data[0]; and then array_add – after a move, first points at freed memory.
  • sizeof p instead of the capacity. A pointer is 8 bytes, however many elements are behind it. Keep the count yourself (count, capacity).
practice 10 cities.c Code Diff
UNCHANGED This step does not change this file.
#include <assert.h>
#include <stdio.h>
#include <string.h>
#define MAX_NAME 20
#define CITY_COUNT 5
/* City: three pieces of data of different types under one name. */
typedef struct {
char name[MAX_NAME];
int population;
double area; /* km2 */
} City;
/* Prints one city. Before: c != NULL. */
void print_city(const City *c) {
assert(c != NULL);
printf("%s: %d people, %.1f km2\n", c->name, c->population, c->area);
}
/* Gets a COPY of the city: the original doesn't change. */
void grow_copy(City c, int count) {
c.population += count;
}
/* Gets a pointer to the city: changes the original. Before: c != NULL. */
void grow(City *c, int count) {
assert(c != NULL);
c->population += count;
}
/* Population density, people per km2. Before: c != NULL, c->area > 0. */
double density(const City *c) {
assert(c != NULL && c->area > 0);
return c->population / c->area;
}
/* The index of the city with this name, or -1.
Before: n >= 0, t and name not NULL. */
int find_city(const City t[], int n, const char *name) {
assert(n >= 0 && t != NULL && name != NULL);
for (int i = 0; i < n; i++) {
if (strcmp(t[i].name, name) == 0) {
return i;
}
}
return -1;
}
/* Sorts by population, largest first, by selection.
Before: n >= 0, t != NULL. */
void sort_by_population(City t[], int n) {
assert(n >= 0 && t != NULL);
for (int i = 0; i < n - 1; i++) {
int max = i;
for (int j = i + 1; j < n; j++) {
if (t[j].population > t[max].population) {
max = j;
}
}
City temp = t[i];
t[i] = t[max];
t[max] = temp;
}
}
int main(void) {
City kaunas = {"Kaunas", 300000, 157.0};
City vilnius = {.name = "Vilnius", .population = 600000, .area = 401.0};
print_city(&kaunas);
print_city(&vilnius);
printf("sizeof(City): %zu\n", sizeof(City));
City copy = kaunas;
copy.population = 0;
printf("kaunas: %d, copy: %d\n", kaunas.population, copy.population);
grow_copy(kaunas, 1000);
printf("After grow_copy: %d\n", kaunas.population);
grow(&kaunas, 1000);
printf("After grow: %d\n", kaunas.population);
City cities[CITY_COUNT] = {
{"Klaipeda", 150000, 98.0}, {"Panevezys", 85000, 50.0}, {"Vilnius", 600000, 401.0},
{"Alytus", 50000, 40.0}, {"Kaunas", 300000, 157.0},
};
sort_by_population(cities, CITY_COUNT);
printf("\nBy population:\n");
for (int i = 0; i < CITY_COUNT; i++) {
printf("%d. ", i + 1);
print_city(&cities[i]);
}
int k = find_city(cities, CITY_COUNT, "Alytus");
if (k != -1) {
printf("Alytus is in place %d, density %.0f people/km2\n", k + 1, density(&cities[k]));
}
if (find_city(cities, CITY_COUNT, "Utena") == -1) {
printf("Utena is not on the list.\n");
}
return 0;
}