Quiz C : Memory Bugs

Manual memory management is where most serious C bugs come from. This hard quiz shows short programs with leaks, double frees, dangling pointers and buffer overruns, and asks you to name the bug and the fix, just like a code review or a C interview.

Difficile 10 questions ~8 minutes Réussi à partir de 80 % C17

Choisissez un mode

Vous jouez en invité : connectez-vous pour garder votre XP et votre série.

Exemple de question

Question 1 sur 10

What is wrong with this code?

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

int main(void) {
    for (int i = 0; i < 3; i++) {
        char *buf = malloc(64);
        if (buf == NULL) return 1;
        snprintf(buf, 64, "line %d", i);
        printf("%s\n", buf);
    }
    return 0;
}
Voir la réponse

C. Memory leak: each buf is never freed Every iteration allocates a new block and loses the pointer at the end of the loop body, so three blocks leak. Add free(buf); after the printf(), or allocate once before the loop.

Ce que couvre ce quiz

  • Memory leaks in loops and when a pointer is overwritten
  • Use after free and dangling pointers
  • Double free and freeing memory that was not allocated
  • Off-by-one allocations for strings
  • Tools that catch these bugs at runtime

Questions fréquentes

Faut-il un compte ?

Non. Tout le monde peut jouer. Connectez-vous pour garder votre XP, votre série, vos badges et votre historique.

Puis-je le refaire ?

Oui. Votre meilleur score est conservé, et les questions comme les réponses sont mélangées à chaque fois.