好的,关于malloc的一些答案已经发布了。
更有趣的部分是free是如何工作的(在这个方向上,malloc也可以更好地理解)。
In many malloc/free implementations, free does normally not return the memory to the operating system (or at least only in rare cases). The reason is that you will get gaps in your heap and thus it can happen, that you just finish off your 2 or 4 GB of virtual memory with gaps. This should be avoided, since as soon as the virtual memory is finished, you will be in really big trouble. The other reason is, that the OS can only handle memory chunks that are of a specific size and alignment. To be specific: Normally the OS can only handle blocks that the virtual memory manager can handle (most often multiples of 512 bytes e.g. 4KB).
所以返回40字节到操作系统将不能工作。那么,免费有什么作用呢?
Free会把内存块放在它自己的空闲块列表中。通常情况下,它还尝试将地址空间中的相邻块融合在一起。空闲块列表只是一个循环的内存块列表,在开始的时候有一些管理数据。这也是为什么使用标准malloc/free管理非常小的内存元素效率不高的原因。每个内存块都需要额外的数据,越小的内存块就会产生越多的碎片。
当需要新的内存块时,malloc首先查看的还是空闲列表。在它从操作系统调用新内存之前,它会被扫描。当发现一个块比所需的内存大时,它被分为两部分。一个返回给调用者,另一个返回到空闲列表中。
对于这个标准行为有许多不同的优化(例如对于小块内存)。但是,由于malloc和free必须是通用的,所以当不可用替代方案时,标准行为总是一个退路。在处理空闲列表方面也有优化——例如将块存储在按大小排序的列表中。但是所有的优化也有其局限性。
为什么你的代码崩溃:
原因是,通过将9个字符(不要忘记后面的空字节)写入大小为4个字符的区域,您可能会覆盖存储在您的数据块“后面”的另一个内存块的管理数据(因为该数据通常存储在内存块的“前面”)。当free试图将您的块放入空闲列表时,它会接触到这个管理数据,因此会碰到一个被覆盖的指针。这会使系统崩溃。
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!
这些是一些最糟糕的C/ c++问题,也是指针问题如此严重的原因之一。