+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/25/2019 7:15:50 AM

Programmer Raja

9 Answers

New Answer

+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

✳AsterisK✳ No output when working with pointers usually means crash. And it is indeed a crash. Incompatible pointer assignment is happening here. The type of "hello" is const char [6], this is a statically allocated string by the compiler. The type of p is char*, thus invalid pointer is being passed to the free function, causing the crash.

+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);