- 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: malloc, calloc & free
This C quiz checks how you use malloc(), calloc(), realloc() and free() to work with memory on the heap. You will predict what small programs print and pick the safe way to allocate, grow and release a block.
Domanda di esempio
Domanda 1 di 10What does this print?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a = malloc(5 * sizeof *a);
if (a == NULL) return 1;
for (int i = 0; i < 5; i++) a[i] = i * i;
int sum = 0;
for (int i = 0; i < 5; i++) sum += a[i];
printf("%d\n", sum);
free(a);
return 0;
}
Mostra la risposta
B. 30 The block holds the squares 0, 1, 4, 9 and 16, which add up to 30. sizeof *a is the size of one int, so 5 * sizeof *a is room for exactly five of them.
Cosa copre questo quiz
- Allocating arrays with
malloc(n * sizeof *p) calloc()vsmalloc(): zeroed memory and the two arguments- Growing a block safely with
realloc() - Checking for
NULLand whatfree(NULL)does - Returning heap memory from a function
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.