1

Does free just go byte by byte and set every byte to NULL? I am just curious as to how free explicitly works.

Edit: I guess I should be a bit more specific, I apologize. I implemented my own version of malloc and am now attempting to implement free. I have the void * returned from malloc and am using that as an argument for my free function obviously. I am trying to figure out what to do with this function and how I would go about coding this. I implemented malloc using an explicit free list so I want to return this block back to the free list which I know how to do I just don't know what to do with the data inside the memory block.

John Ron
  • 55
  • 6
  • 1
    Why would it need to set bytes to anything? The pointed to memory block is simply returned to the allocator, there is no need to do anything else with it – UnholySheep Nov 19 '21 at 14:38
  • 3
    Does this answer your question? [How do free and malloc work in C?](https://stackoverflow.com/questions/1957099/how-do-free-and-malloc-work-in-c) Or [How do malloc() and free() work?](https://stackoverflow.com/questions/1119134/how-do-malloc-and-free-work) – pzaenger Nov 19 '21 at 14:39
  • Re "*I am just curious as to how free explicitly works.*", It simply stops marking the memory as used, or equivalent. See linked post for details. // Re "*Does free just go byte by byte and set every byte to NULL?*", It could do that if it wanted, but it's under no obligation to do so, and I'm not familiar with any implementation that does this. – ikegami Nov 19 '21 at 14:42
  • @UnholySheep: Re “Why would it need to set bytes to anything?”: Because it can assist with debugging. Some memory management allocations have debugging mode features that overwrite freed memory with various patterns. – Eric Postpischil Nov 19 '21 at 14:46
  • I guess I should be a bit more specific, I apologize. I implemented my own version of malloc and am now attempting to implement free. I have the void * returned from malloc and am using that as an argument for my free function obviously. I am trying to figure out what to do with this function and how I would go about coding this. I implemented malloc using an explicit free list so I want to return this block back to the free list which I know how to do I just don't know what to do with the data inside the memory block. @UnholySheep – John Ron Nov 19 '21 at 14:46
  • @ikegami I believe (at least older versions of) VC++ does so in debug-mode. – Morten Jensen Nov 19 '21 at 14:47
  • @Morten Jensen, Interesting, thanks – ikegami Nov 19 '21 at 14:50
  • Re "*I just don't know what to do with the data inside the memory block*", Nothing. Well, nothing needs to be done. If you want to do something, that's fine too. – ikegami Nov 19 '21 at 14:50
  • @ikegami so technically just add it back to the free list and then overwrite the data when that memory block is being used in malloc again? – John Ron Nov 19 '21 at 14:52
  • malloc doesn't need to overwrite either. And usually doesn't. Unlike calloc, it doesn't guarantee "clean" memory. – ikegami Nov 19 '21 at 14:54
  • @ikegami ahhhh okay. appreciate the insight – John Ron Nov 19 '21 at 14:56

1 Answers1

2

The C standard defines the behavior of the free function:

The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation.

In other words, it doesn't really matter what exactly happens to it. What matters is it's available for further malloc().

Loqz
  • 373
  • 4
  • 16