+ 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

25th Aug 2019, 7:15 AM
Programmer Raja
Programmer Raja - avatar
8 Respuestas
+ 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");)
25th Aug 2019, 7:38 AM
daniel
daniel - avatar
+ 9
Additionally, assigning p to NULL doesn't solve all problems as the memory leak is still there. Use strcpy as daniel said.
25th Aug 2019, 10:48 AM
Sonic
Sonic - avatar
+ 5
i don't think this is the case of dangling pointer Programmer Raja , ~ swim ~ shed some light please
25th Aug 2019, 9:30 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 4
daniel yeah it's work thanks
25th Aug 2019, 7:43 AM
Programmer Raja
Programmer Raja - avatar
+ 4
Sonic ok bro
25th Aug 2019, 11:15 AM
Programmer Raja
Programmer Raja - avatar
+ 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
25th Aug 2019, 7:33 AM
Programmer Raja
Programmer Raja - avatar
+ 2
You cant assign the text to 'p' in that manner. Maybe do sprintf(p, "hello"); or strcpy(p, "hello");
25th Aug 2019, 1:21 PM
Joshua Sledden
Joshua Sledden - avatar
+ 1
Don't use free(p);
27th Aug 2019, 4:21 AM
Kota Lalithaditya
Kota Lalithaditya - avatar