![908ae27f9af57a7319b2bcfa84c207d6.png](https://i-blog.csdnimg.cn/blog_migrate/fc206613f377ca335bb0c17ea87a6ae0.png)
free()函数入参是一个void*指针,它是如何知道被指向的大小的?
再stackoverflow上看到了这样一篇帖子:https://stackoverflow.com/questions/1518711/how-does-free-know-how-much-to-free
Most implementations of C memory allocation functions will store accounting information for each block, either in-line or separately.
One typical way (in-line) is to actually allocate both a header and the memory you asked for, padded out to some minimum size. So for example, if you asked for 20 bytes, the system may allocate a 48-byte block:
- 16-byte header containing size, special marker, checksum, pointers to next/previous block and so on.
- 32 bytes data area (your 20 bytes padded out to a multiple of 16).
The address then given to you is the address of the data are