To learn more about pointers I wrote just a simple test function which creates a pointer, allocates space ande after some output on the shell the space shall be freed.
void main() {
int *p = (int*) malloc(sizeof(int));
int a = 42;
printf("p: %x\n", p);
printf("*p: %d\n", *p);
printf("a: %s\n", a);
printf("&a: %x\n", &a);
p = &a;
printf("p: %x\n", p);
printf("*p: %x\n", *p);
//Until now everything works as expected
free(p); //ERROR
// printf("p: %x\n", p); // Shall cause error, because space was freed
// printf("*p: %x\n", *p); // Shall cause error, because space was freed
}
At first runs everything was ok. free(p) caused no error. Then I tried the same test with a struct and got the double free error. "Ok, maybe I do something wrong, lets go to the start", I thought and Ctrl+Z everything to this one above, like it was before. Now I still get this error. Why? Thats a newbie question, I know. The code above you can find everywhere in the web as a simple demonstration of malloc and free. Like here: http://www.cplusplus.com/reference/cstdlib/free/ Hope, someone can tell me what I do wrong.