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

Discussion

On freeing a dynamic memory, if the pointer value is not modified, then the pointer points to?

  • A.NULL
  • B.Other dynamically allocated memory
  • C.The same deallocated memory location
  • D.It points back to location it was initialized with

Answer: C

Correct answer: The same deallocated memory location

free receives the pointer by value and cannot alter the caller's copy, so the bits in the pointer are exactly what they were. It still holds the address of the block - which now belongs to the allocator again.

This is a dangling pointer, and it is dangerous precisely because it looks valid and may appear to work while the freed block is untouched. Setting the pointer to NULL immediately after freeing is the standard discipline, since a null dereference fails immediately and visibly.

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

Post your comment

Please log in or register to post a comment.