Search code examples
mallocfreememset

C - free() or memset() to zero and free()?


Hi I'm just reading all the Q&A here on stackoverflow but can't find the exact information whether to only free() memory allocated by malloc() or to set it to zero by memset() and then free() to avoid potential further usage of the data (because afaik free() leaves the data intact) (avoid any other program to read the data when I left them). Thanks


Solution

  • Some compilers like the Microsoft compiler will set the values to 0xfeeefeee so that you can inspect the memory and see if it's been freed. This is useful for debugging and finding problems with accessing memory addresses no long valid, and you can also add asserts to help find these problems at runtime.