Search code examples
cmemory-managementfree

free() not deallocating memory?


free(str);
printf("%d\n", str->listeners);

The call to printf succeeds (as do any other calls to str's members). How is this possible?


Solution

  • You're just (un)lucky. That code exhibits undefined behavior - anything can happen, including looking like the memory wasn't freed.

    The memory is freed, but there is no point in actively clearing it, so its original content is likely to still be there. But you can't rely on that.