- Ausführende
-
Lernen
Python lernen Ausführbare Beispiele, NumPy, pandas und mehr KI / Machine Learning lernenNeuronale Netze, ML und LLMs von Grund auf C/C++ lernen Zeiger, Speicher, STL und modernes C++ C# lernenC#-Grundlagen, OOP, LINQ, Async und .NET-Übungen Matplotlib lernenLinien-, Balken-, Streu- und Kreisdiagramme mit interaktiven Visualisierungen Rust lernenOwnership, Traits, Fehlerbehandlung und Threads Go lernenSlices, Interfaces, Goroutinen und Channels Python-Algorithmen Suche, Sortierung, Rekursion und Visualisierungen O(n) C/C++-Algorithmen Implementierungen in C und C++ vergleichen Programmieren für Kinder Spielend programmieren lernen!
- QuizzeNeu
- AufgabenNeu
- Für LehrkräfteNeu
- Aktualisierungen
- Spendier mir einen Kaffee
C-Quiz: 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.
Beispielfrage
Frage 1 von 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;
}
Lösung zeigen
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.
Darum geht es in diesem 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
Häufige Fragen
Brauche ich ein Konto?
Nein. Jeder kann spielen. Mit Anmeldung werden XP, Serie, Abzeichen und Verlauf gespeichert.
Kann ich es wiederholen?
Ja. Dein Bestwert bleibt erhalten, und Fragen und Antworten werden jedes Mal neu gemischt.