0 0 votes Something wrong with the code or is it working fine ? Programming in C + – HeadShot 1.9k views answer comment Share Follow Print See all 16 Comments 16 16 Comments reply Shaik Masthan commented Aug 10, 2018 reply Follow flag didn't find anything wrong in it, i hope it works fine.... what's your doubt on it? 0 0 replyShare srestha commented Aug 10, 2018 reply Follow flag there is a problem I think, memory is not deallocated 0 0 replyShare Shaik Masthan commented Aug 10, 2018 reply Follow flag @srestha, free(ptr) ---- in else part...... may be you missed it mam 0 0 replyShare arvin commented Aug 10, 2018 reply Follow flag it will execute normal. no error :) 0 0 replyShare srestha commented Aug 10, 2018 reply Follow flag @Shaik I havenot told pointer freed. I told about memory deallocation 0 0 replyShare Shaik Masthan commented Aug 10, 2018 reply Follow flag @ srestha, mam what is difference between memory de-allocation and pointer freed According to me, both are same.. 0 0 replyShare Abhishek_g commented Aug 10, 2018 reply Follow flag I guess there will be no error.although there might be warning about implicit declaration of character pointer if you don't include stdlib.h 0 0 replyShare srestha commented Aug 10, 2018 reply Follow flag @Shaik If we forgot memory deallocation, then there will be memory leak error but in case if we forgot to free pointer, then there will be dangling pointer error right? 0 0 replyShare HeadShot commented Aug 10, 2018 reply Follow flag @srestha I think , ptr stores the starting address of allocated memory hence "free(ptr) " will deallocate the memory successfully. P.S : free (pointer_var) may not used to avoid dangling pointer but pointer_var = NULL does. Please correct if I am wrong. 0 0 replyShare HeadShot commented Aug 10, 2018 reply Follow flag @Shaik Masthan I thought of its working is fine but doubted if I might miss some of the point. There are options like, error in code, error free, error free but warning etc 0 0 replyShare Abhishek_g commented Aug 10, 2018 reply Follow flag From what i know, 1) When we free an area of memory, but still keep a pointer to it, that pointer is dangling: we didn't use ptr after it was freed.so its not dangling pointer 2) When we lose the pointer, but keep the memory allocated,then we have a memory leak: memory is allocated in main() so definately not this case as well https://stackoverflow.com/questions/13132798/difference-between-dangling-pointer-and-memory-leak 1 1 replyShare Shaik Masthan commented Aug 10, 2018 reply Follow flag @srestha, mam LEARNED a new thing from your comment but what you are saying is wrong #include <stdio.h> #include <stdlib.h> int main() { int i=7; printf("i address...%u\n",&i); int *p=(int *)malloc(sizeof(int)); printf("p points to address ...%u\n",p); if(p==0) return ; else { *p=12; printf("*the value pointed by p...%d\n",*p); free(p); // free up the memory pointed by p ===> used for to stop memory leaking printf("*p...%d\n",*p); // causes dangling pointer printf("p address after free ... %u\n",p); // pointing to same address } p=&i; printf("after assigning i to p...%u\n",p); printf("Hello World...%u\n",*p); return 0; } 0 0 replyShare srestha commented Aug 10, 2018 reply Follow flag @ Abhishek_g I cannot understand ur argument If memory still required, why r we freeing 10? 0 0 replyShare srestha commented Aug 11, 2018 reply Follow flag @Shaik where I told wrong? 0 0 replyShare Shaik Masthan commented Aug 11, 2018 reply Follow flag @srestha,mam your first comment there is a problem I think, memory is not deallocated memory successfully de-allocated. in case if we forgot to free pointer, then there will be dangling pointer error there is no concept of free the pointer.... we have re-initialize it, if we want to use it again 0 0 replyShare srestha commented Aug 11, 2018 reply Follow flag without freeing pointer there is a dangling pointer issue 0 0 replyShare Please log in or register to add a comment.