2,768 views
0 0 votes
operations :
(i) Pointer p1 is set to point at a new heap-dynamic variable.
(ii) Pointer p2 is assigned p1’s value.
(iii) The heap dynamic variable pointed to by p1 is explicitly de-allocated, but p2 is not changed by the operation.

This situation leads to which of the following :

(A) p1 becomes a dangling pointer
(B) p2 becomes a dangling pointer
(C) Both p1 and p2 are now dangling pointers
(D) Neither p1 nor p2 is now a dangling pointer

1 Answer

Best answer
3 3 votes
Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the de-allocated memory.

Since P2 is pointing the memory location that has been de-allocated .So we can say P2 becomes dangling pointer.
selected by
Answer:
Position:
Show:

Related questions

4 4 votes
2 2 answers
289
289 views
GO Classes asked Jun 27
289 views
What will happen in the following code?#include <stdio.h #include <stdlib.h int main() { int *p = (int *)malloc(sizeof(int)); *p = 10; free(p); printf("%d", *p); return 0...
1 1 vote
2 answers 2 answers
1.3k
1.3k views
Souvik33 asked Nov 28, 2022
1,289 views
MSQ Consider the following C snippet:#include <stdio.h int main() { int *ptr = (int*) malloc(100*sizeof(int)); *ptr=33; printf("%d %d\n",ptr,*ptr); // Line X free(ptr); *...
0 0 votes
1 1 answer
1.5k
1.5k views
Gurdeep Saini asked Feb 1, 2019
1,492 views
is it dangling pointer ?int main(void) { int* p;printf("%d",*p); return 0;} https://ideone.com/IN77el
0 0 votes
1 1 answer
1.3k
1.3k views
iarnav asked May 5, 2017
1,273 views
#include <stdio.h int*fun() { intx=5; return&x; } int main(){ int*p=fun(); fflush(stdin); printf("%d",*p); return0; }Please explain this code, how's it's working line by ...