The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+21 votes
1.8k views

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:

  1. $X - 1 \ \  Y - 3 \ \  Z - 2$
  2. $X - 2 \ \  Y - 1 \ \  Z - 3$
  3. $X - 3 \ \  Y - 2 \ \  Z - 1$
  4. $X - 3 \ \  Y - 1 \ \ Z - 2$
asked in Programming by Veteran (59.6k points)
edited by | 1.8k views

1 Answer

+46 votes
Best answer

Answer is (D).

$X: m = NULL$; makes the pointer $m$ point to $NULL$. But the memory created using $malloc$ is still there and but cannot be used as we don't have a link to it. Hence, lost memory

$Y: n$ is freed and so pointer $n$ is now pointing to an invalid memory making it a Dangling pointer.

$Z: p$ is not initialized. $p = malloc(sizeof(char))$; should have been used before assigning '$a$' to $*p$.

answered by Loyal (5.3k points)
edited by
0
@Vasudha-Garg
0
nice explanation....
0
pls explain option a ) little bit more
+13
m=malloc(5)  //m is pointing to memory location allocated by malloc function

m=null         //  now m is not pointing to anything, but memory is still allocated and now we dont have a link to access  the memory

therefore memory Lost.
0
well explained
0
Nice
0

For more information about Dangling pointer and uninitialized pointer please refere to https://www.geeksforgeeks.org/dangling-void-null-wild-pointers/

0
Case X is also known as Memory Leak
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

42,415 questions
48,474 answers
154,483 comments
62,892 users