#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; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter