-2
int main(int argc, char **argv)
{
    counter = 0;
    int size = 5;
    struct trie *mainTrie = malloc(size * sizeof(mainTrie));
    test(mainTrie);
    printf("%c", mainTrie[2].alphabet);
    free(mainTrie->alphabet);
    printf("%c", mainTrie[2].alphabet);
    return 0;
}

The test functions was just to see how I can use malloc. My experiment was successful except one thing: free(mainTrie).

When I added printf("%c", mainTrie[2].alphabet) after I 'freed' the memory spaces, the output still gave me the same letter that was stored at mainTrie[2].alphabet from method 'test'.

Am I not understanding something? Thanks in advance...

ifnDef
  • 101
  • 8
  • 1
    What did you expect to happen, and why? – M.M Oct 15 '14 at 23:46
  • 1
    `struct trie *mainTrie = malloc(size * sizeof *mainTrie );` – wildplasser Oct 15 '14 at 23:58
  • `free()` did deallocate the allocated memory. Your attempt to access that memory after it was deallocated has undefined behavior. Just don't do that. – Keith Thompson Oct 15 '14 at 23:58
  • @wildplasser: Yes. It's also worth explaining that the `malloc` call in the question uses the wrong size. It allocates space for 5 pointers, not for 5 `struct trie` objects. `malloc(size * sizeof(struct trie))` would also be correct but your `malloc(size * sizeof *mainTrie)` is equivalent and more robust. – Keith Thompson Oct 16 '14 at 00:00
  • Also http://stackoverflow.com/questions/20801582/i-call-free-but-the-pointer-still-has-data-and-its-content-hasnt-changed and many many many other questions about the same thing. – AnT stands with Russia Oct 16 '14 at 00:01
  • I threw some stuff in the trash, but when I looked in the trashcan it was still there. Am I not understanding something? – Jim Balter Oct 16 '14 at 00:25
  • Sorry, @KeithThompson ,it seemed too trivial, that's why it was only a comment. Any method of debugging could detect it. – wildplasser Oct 16 '14 at 00:38

1 Answers1

2

Show us your complete code, specially the maintree struct. It seems like you need to free the maintree variable:

free(maintree);

However, freeing memory means that the piece of memory you reserved will be available to the O.S again. It doesn't mean you actually set that piece of memory tu NULL. Edit: @MattMcNabb "Typically the memory is not released to the OS, just made available for further allocations from the same process. (This depends on a lot of things of course)"

It is possible that you are printing a piece of memory that doesn't belong to your program anymore, but the data hasn't changed yet.

Note these 2 important things from the documentation

  1. A block of memory previously allocated by a call to malloc, calloc or realloc is deallocated, making it available again for further allocations. (just deallocated, doesn't say anything about the values being changed)

If ptr does not point to a block of memory allocated with the above functions, it causes undefined behavior.

If ptr is a null pointer, the function does nothing.

  1. Notice that this function does not change the value of ptr itself, hence it still points to the same (now invalid) location. (so you maybe pointing to a piece of memory you allocated in the past, but it still has the same values, it is possible, it wont happen all the time).
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
  • 5
    Typically the memory is not released to the OS, just made available for further allocations from the same process. (This depends on a lot of things of course) – M.M Oct 15 '14 at 23:46
  • thanks @MattMcNabb for the correction! I agree with you. – Cacho Santa Oct 15 '14 at 23:47
  • Always incorporate corrections into your answer ... it's not enough to just have them in the comments. – Jim Balter Oct 16 '14 at 00:29