- Esecutori
-
Impara
Impara Python Esempi eseguibili, NumPy, pandas e altro Impara l'IA / Machine LearningReti neurali, ML e LLM da zero Impara C e C++ Puntatori, memoria, STL e C++ moderno Impara C#Fondamenti, OOP, LINQ, async ed esercizi .NET Impara MatplotlibGrafici a linee, barre, dispersione e torta con visualizzatori Impara RustOwnership, trait, gestione degli errori e thread Impara GoSlice, interfacce, goroutine e canali Algoritmi Python Ricerca, ordinamento, ricorsione e visualizzazioni O(n) Algoritmi C e C++ Confronta le implementazioni in C e C++ Programmazione per bambini Impara a programmare giocando!
- QuizNuovo
- SfideNuovo
- Per docentiNuovo
- Aggiornamenti
- Offrimi un caffè
Quiz di 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.
Domanda di esempio
Domanda 1 di 10What 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;
}
Mostra la risposta
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.
Cosa copre questo 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
Domande frequenti
Serve un account?
No. Chiunque può giocare. Accedi per salvare XP, serie, badge e cronologia.
Posso rifarlo?
Sì. Il tuo miglior punteggio resta salvato, e domande e risposte vengono mescolate ogni volta.