+ 3
Why no output fir this program?
Even though i write print statement it not print in output but if i comment a line free it work fine https://code.sololearn.com/cyix81Ak2IIL/?ref=app
8 Answers
+ 12
Because in line 7 you assign to p a pointer to the string "hello", loosing the path to the allocated memory (a memory leak).
Use the strcpy function to assign to the memory pointed by p instead of p itself.(strcpy(p,"hello");)
+ 9
Additionally, assigning p to NULL doesn't solve all problems as the memory leak is still there. Use strcpy as daniel said.
+ 5
i don't think this is the case of dangling pointer Programmer Raja , ~ swim ~ shed some light please
+ 4
daniel yeah it's work thanks
+ 4
Sonic ok bro
+ 3
Yeah i find why there is no output after spending some time.
It is due to dangling pointer
If we give p=Null
Before we free the memory
It's work's fine
https://code.sololearn.com/c9cyBYm54ppT/?ref=app
+ 2
You cant assign the text to 'p' in that manner.
Maybe do sprintf(p, "hello"); or strcpy(p, "hello");
+ 1
Don't use free(p);