Machine-translated by Google. Questions are authored in English.
Top

Discussion

The most appropriate matching for the following pairs

X: m=malloc(5); m= NULL;        1: using dangling pointers
Y: free(n); n->value=5;         2: using uninitialized pointers
Z: char *p; *p = 'a';           3. lost memory is:

  • A.X-1 Y-3 Z-2
  • B.(X-2 Y-1 Z-3
  • C.X-3 Y-2 Z-1
  • D.X-3 Y-1 Z-2

Answer: D

Correct answer: X-3 Y-1 Z-2

Take each line in turn. X allocates memory and then overwrites the only pointer to it with NULL, so the block can never be freed - that is lost memory, a leak, giving X-3.

Y frees the block and then writes through the same pointer, which now refers to memory the allocator has reclaimed - a dangling pointer, giving Y-1. Z declares a pointer without ever assigning it an address and writes through it, which is an uninitialised pointer, giving Z-2. The three are the classic pointer faults.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.