The most appropriate matching for the following pairs is:
View worked solution
Answer: D
We can determine the correct matching by analyzing each scenario step-by-step:
-
Scenario X:
m = malloc(5); m = NULL;
Memory of bytes is dynamically allocated on the heap, and its address is stored in the pointerm. Whenmis reassigned toNULLwithout freeing the allocated memory, the program loses the reference to that heap memory. This results in a memory leak, also known as lost memory.
-
Scenario Y:
free(n); n -> value = 5;
The memory block pointed to bynis deallocated usingfree(n). After deallocation,nstill holds the address of that memory block, making it a dangling pointer. Attempting to access or modify members usingn->valueis an operation on a dangling pointer.
-
Scenario Z:
char *p; *p = 'a';
The pointerpis declared but not initialized to point to any valid memory address (it contains a garbage value). Dereferencing it with*p = 'a'attempts to write to an arbitrary memory location, which is a classic case of using an uninitialized pointer.
Combining these matches, we get: This corresponds to Option D.
Free practice · Your answers on this page are not saved.
