C
Chris Dollin
Mark said:Chris said:KG wrote:
Hi,
I do have a question.
int main()
{
char *p = (char *)malloc(9);
strcpy(p,"TajMahal");
p++;
free(p);
}
Does this program results in memory leak?
It results in undefined behaviour. The only legal arguments to `free`
are null and a pointer returned from malloc that hasn't been freed.
There's no way of telling what that does from the C standard.
If you omit the `p++`, then the program terminates with memory
still allocated. [snip]
Did you really mean this?
I really meant it.
Now, of course, I see it as nonsense. What I intended to say was more
like "If you omit the `p++` /and the free/", then ...", but a demon
flew out of my nose and erased the "and the free" bit.
Thanks for catching my idiocy, Mark.