C programming · CM10 · 90 minutes
ISTIC · University of Rennes
2026–2027
$ whoamiNot a C expert … :')
Associate Professor
University of Rennes · ISTIC
IRISA / Inria · Taran
Previously: Inria / INSA Lyon, Emeraude.
PhD: BSC / UPC.

MareNostrum 4 · BSC, Barcelona
Dynamic memory allocation on the heap
We open an image and discover its dimensions. Where do we store its pixels?
Can we change the copy and keep the original?
Keep hello in the original.
Change its copy to Hello.
char *copy = original; copies the address.
Read the report from the corrected allocation.
Which issue remains?
6 bytes definitely lost
We allocated the copy but never released it.
Before free: changing copy[0] leaves original[0] unchanged.
We copied existing characters. Now we create an array of values.
How many bytes? What values are there before we write?
We need space for 100 integers, initially set to zero.
How many bytes should we request?
What happens if we remove the loop that writes zeros?
free?Move array creation into a function. Can its caller use the array?
During the call
values exists and contains four zeros.
After the return
values no longer exists. p is dangling, without any free.
Returning an address does not keep a local object alive.
The pointer and the object it refers to have separate lifetimes.
| Storage | Lifetime ends |
|---|---|
| Ordinary local array | When its block ends |
Block from malloc |
When it is released with free |
Global or static object |
When the program ends |
Each recursive call has its own locals. Too many calls can exhaust the stack.
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.
<stdlib.h>We allocated, filled and released a block. We can also request zeros or resize it.
| 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 |
free contractAnswer questions 1–5, then discuss your reasoning.
We know how to allocate one row. How can we store a whole grid?
Answer questions 6–8, then discuss your reasoning.
An array groups values of one type. How can one object hold a street name and a house number?
One address contains a street name and a house number.
struct: group the street and number.enum: name integer constants.typedef: give a type another name.How many objects exist? Are their members ready to read?
These definitions allocate two distinct objects of the same type.
Local objects need initialisation
Ordinary local members have indeterminate values until initialised or assigned. The type definition alone sets no values.
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