dangling pointer
Sat Jun 11 2022 06:18:12 GMT+0000 (Coordinated Universal Time)
Saved by
@KanishqJ8
#include <stdlib.h>
int *functionDangling(){
int a,b,sum;
a=34;
b=4;
sum=a+b;
return ∑
}
int main(){
//case1: deallocation a memory block
int *ptr=(int*)malloc(7*sizeof(int));
ptr[0]=34;
free(ptr);//ptr is now a dangling pointer
//case2: function returning local variable address
int *dangPtr=functionDangling(); //ptr is now a dangling pointer
//case3: if variable goes out of scope
int*danglingPtr3;
{
int a=8;
danglingPtr3=&a;
}
//here variable a goes out of scope which means danlingPtr3 is pointing to a location which is free and hence it is now a dangling pointer
return 0;
}
content_copyCOPY
Comments