0

This is some sample code, but it basically shows my Problem (i wrote this freestyle without compiling it so it might have some syntax error but it's just about the idea).

typedef struct Person {
    char name[25];
}Person;


int main() {
    Person *p = malloc(sizeof(Person));
    strcpy(p->name,"Philip");
    free(p);
    printf("%s",p->name); /* prints Philip but why? */
    return 0;
}

So i'm allocating space for p and after i gave it the name of "Philip" i free() it. So, in my understanding, p->name should be NULL. Am i using free incorrectly? Isn't it "bad" that the string 'name' still stays in my ram?

Philip Feldmann
  • 7,887
  • 6
  • 41
  • 65
  • 1
    See http://stackoverflow.com/a/6445794/34397 – SLaks Nov 23 '14 at 13:33
  • It's obviously not the full program and it's also, as i've said, just some code i wrote freestyle. I just want to know how to free the "attributes" of a struct pointer that i deleted, since they appear to stay in memory. – Philip Feldmann Nov 23 '14 at 13:34
  • Read http://stackoverflow.com/a/6445794/34397, which precisely explains your misconception. – SLaks Nov 23 '14 at 13:35

1 Answers1

0

So, in my understanding, p->name should be NULL

Why should it be NULL? Does the documentation of free() say anywhere that this function zeroes out the underlying memory of the freed buffer? No. Then what's the question?

Accessing the memory behind a free()'d pointer is undefined behavior. Don't do it. And if you do it, don't be surprised by any result.

  • Alright, that's my misconception there. Does it mean that the old object is still in my memory but can be overwritten by anything after i use free? And is it enough for a "good" memory-managed program to free only the pointer of a struct or do i need to free all the attributes as well? – Philip Feldmann Nov 23 '14 at 13:42
  • @PhilipFeldmann of course it's still in memory. `malloc()` & co. can only return pointers to locations which are already present in memory. Allocation simply means that you request a pointer from the OS to memory that hasn't been marked as used by anyone else. Allocator functions can't just go ahead, install some more RAM in your computer and return a pointer to "newly allocated" memory… `free()`ing a pointer simply means that you give it back to the OS, promising that you won't use it anymore. – The Paramagnetic Croissant Nov 23 '14 at 13:48
  • @PhilipFeldmann Deallocation doesn't make physical memory disappear from your system. After freeing, the OS does with it whatever it pleases, including re-using it or not re-using it. As to the other question: you free **if and only if** you allocated. You will have exactly one `free()` per allocation. In other words, you `free()` if you have previously called `malloc()`, `realloc()`, `calloc()` or any other function that called these, and you do **NOT** free otherwise. You should have exactly one `free()` per dynamic allocation. – The Paramagnetic Croissant Nov 23 '14 at 13:50
  • @PhilipFeldmann If your struct contains a pointer that was allocated dynamically, then you definitely do need to free that pointer (**first,** *before* you free its containing struct). If you have a pointer that has **not** been allocated using these functions, then you **do not** free it. It's that simple. – The Paramagnetic Croissant Nov 23 '14 at 13:55