Consider the following code:
int main()
{
char* s = (char*) malloc(sizeof(char)*10);
s="hello";
free(s);
}
When executing this program I get an error:
** glibc detected *** free(): invalid pointer: 0x0000000000400b2c
My research on this error indicates it may be caused by not assigning enough memory space via malloc(). But the program already calls malloc(), producing enough space for 10 chars.
You are assigning another address to s, to a statically allocated memory. Freeing it is not correct. Also, since you are doing this, you are basically leaking the memory you have allocated here:
Try: