Classify blocks by reachability at exit

A C program is run with valgrind --leak-check=full. When main returns, the heap and the program's pointers are in this state:

  • A global variable cache holds the start address of a 64-byte block.
  • A function load_list built a linked list of three 24-byte nodes and returned without freeing them. Its local head pointer is gone, and no pointer to the first node remains anywhere in memory. The first node's next field holds the start address of the second node, and the second node's next holds the start address of the third.
  • A 100-byte block was allocated into a local buf that has since gone out of scope. The only remaining pointer into that block is the global cursor, which was set to buf + 40.

Fill in the leak category Memcheck reports for each block. Type each category exactly as Memcheck's leak summary names it: definitely lost, indirectly lost, possibly lost or still reachable.

BlockSize (bytes)Memcheck category
Cache block64
First list node24
Second list node24
Third list node24
Buffer block100

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More GDB & Valgrind Questions: