C programming · Structures and dynamic memory
ISTIC · University of Rennes
2026–2027
Dynamic memory allocation · q1–q10
Match X, Y and Z to the problems.
free(NULL)free(NULL) does nothing (CM, slide 291).<stdlib.h> is a separate C17 error.What is the problem with this code?
free() can’t be applied on NULL pointerfree() is called for NULL pointer.free()What is the output?
free() returns void: no value to assign.ptr2 is also uninitialised.Which expression replaces ????
*(arr + i*c + j)arr[i][j]arr + i*c + j*(&arr[i]+&arr[j])i rows of c integers, then j integers.arr[i*c + j].Which expression replaces ?????
*(arr+i*c+j)*(*(arr + i*c) + j)*(*(arr + i*c + j))*(*(arr+i)+j)*(arr+i) selects row i; +j selects column j.arr[i][j] or (*(arr+i))[j].#include <stdio.h>
#include <stdlib.h>
int main() {
int r = 3, c = 4, i, j, count;
int **arr = (int **)malloc(
r * sizeof(int *));
for (i = 0; i < r; i++)
arr[i] = (int *)malloc(
c * sizeof(int));
count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
printf("%d ", arr[i][j]);
free(arr);
return 0;
}What is the output?
m.name does not change s.name.void main() is accepted; strict compilation can reject it. Use int main(void).What is the output?
*p.x++: . binds before *.p is a pointer. p->x++ or (*p).x++ prints 1, then sets x to 2.Each list node will hold a song ID and a pointer.
A structure groups them together.
A listener requests a song next.
Can we insert it without shifting the rest of the playlist?
head still gives A B C D.Insert 101 into [100, 103, 105, 200]. What must change?
Turn the drawn playlist into a working C program: A → B → NULL.
next.head.struct list_elem is already known inside the braces.next stores a pointer. The node’s full size is not needed yet.list_elem_t names the same type.next stores its address; the nodes need not be adjacent.head contain to reach it?node receives the address of the heap block.head is still NULL. ? means uninitialised.node != NULL.node moves to the new allocation. head still reaches A.next.head?p.p reaches B. head still reaches A.Creating each song repeats the same allocation code.
Let create_element() do that work for us.
create_element('A')malloc() reserves the nodevalue and nextmain()We want insert_head(...) to put a new song first.
How can it update head in main()?
head?ll = node changes only lheadhead through *lhead through *lmain(): head is updatedA new song must fit before, between, or after the existing songs.
Which links need to change?
head is our entry point to the existing chain.head firsthead.head to Eprevious points to B.tail points to D.NULL.A song is removed from the playlist.
Reconnect the remaining songs and release its memory.
next link cannot take us back to C.What extra address should each node store?
prev: reach the predecessor directly to unlink a known node.\(n\) nodes; current node known, predecessor not saved.
| Operation | Singly linked | Doubly linked |
|---|---|---|
| Find its predecessor | \(O(n)\) | \(O(1)\) |
| Unlink this node | \(O(n)\) | \(O(1)\) |
| Find node number \(k\) | \(O(n)\) worst case | \(O(n)\) worst case |
| Total node storage | \(\Theta(n)\) | \(\Theta(n)\) |
head, follow next to the predecessor.prev directly; one extra pointer per node.Choose A, B, C or D, then explain why.
head points to A; node points to a separate node E.
After prepend(head, node), what happens to the caller’s head?
NULL.head is unchanged.victim points to the first node of a nonempty list.
A doubly linked list has \(n\) nodes; the current node is known.
Reading prev: what time cost and total node storage?