C programming · CM10 · 90 minutes
ISTIC · University of Rennes
2026–2027
A typical layout, not a C language guarantee. Not to scale.
Static storage
Globals and static objects exist for the whole program. Storage is reserved before execution.
Automatic storage
An ordinary local exists during its block execution. Each recursive call gets its own locals.
The implementation manages their storage. Deep recursion can exhaust the stack.
freeEnd the allocation's lifetime
Pass the start address of a live allocation from malloc, calloc or realloc. free returns no value.
free(NULL) has no effect. Free each allocation once.
Invalid or repeated free is undefined behaviour.
<stdlib.h>| Function | Purpose |
|---|---|
malloc(bytes) |
Allocate uninitialised storage |
calloc(count, size) |
Allocate storage with all bits zero |
realloc(p, bytes) |
Resize an allocation; it may move |
free(p) |
Release an allocation |
C: explicit responsibility
For every successful allocation, decide who will release it and when. Repeated leaks consume more memory.
Java: garbage collection
The runtime can reclaim unreachable objects automatically.
Answer questions 1–5, then discuss your reasoning.
The literals are separate from the lost heap block. Neither pointer can now be passed to free.
Answer questions 6–8, then discuss your reasoning.
Three tools
enum names integer constants. struct groups members. typedef introduces a name for a type.
Use them to describe more complex data, including list nodes.
Padding
The implementation may insert unused bytes between members and at the end to satisfy alignment requirements.
sizeof(struct T) includes padding.16 vs 20 bytes in this layout. Measure with sizeof and offsetof.
typedef: a name for a type