Questions about Linked Lists?

Free fn

Free fn

par Ali Ismail,
Nombre de réponses : 1
Avatar GroupEn

Doctor what did you mean by "destroy the free list" if we call the free fn to an unallocated memory (in the video) 

En réponse à Ali Ismail

Re: Free fn

par Siba Haidar,
The heap manager, manages a free list. If you use free inproperly, it will loose the info about what is free and what is allocated.
Check the following link for more info: https://stackoverflow.com/questions/1119134/how-do-malloc-and-free-work.
[...]

Free will put the memory block in its own free block list. Normally it also tries to meld together adjacent blocks in the address space. The free block list is just a circular list of memory chunks which have some administrative data in the beginning. This is also the reason why managing very small memory elements with the standard malloc/free is not efficient. Every memory chunk needs additional data and with smaller sizes more fragmentation happens.

The free-list is also the first place that malloc looks at when a new chunk of memory is needed. It is scanned before it calls for new memory from the OS. When a chunk is found that is bigger than the needed memory, it is divided into two parts. One is returned to caller, the other is put back into the free list.

There are many different optimizations to this standard behaviour (for example for small chunks of memory). But since malloc and free must be so universal, the standard behaviour is always the fallback when alternatives are not usable. There are also optimizations in handling the free-list — for example storing the chunks in lists sorted by sizes. But all optimizations also have their own limitations.

[... ]
This is a rather graceful behaviour. I have also seen situations where a runaway pointer somewhere has overwritten data in the memory-free-list and the system did not immediately crash but some subroutines later. Even in a system of medium complexity such problems can be really, really hard to debug! In the one case I was involved, it took us (a larger group of developers) several days to find the reason of the crash -- since it was in a totally different location than the one indicated by the memory dump. It is like a time-bomb. You know, your next "free" or "malloc" will crash, but you don't know why!

Those are some of the worst C/C++ problems, and one reason why pointers can be so problematic.

source: https://stackoverflow.com/questions/1119134/how-do-malloc-and-free-work