If free() knows the length of my array, why can’t I ask for it in my own code?

Besides Klatchko’s correct point that the standard does not provide for it, real malloc/free implementations often allocate more space then you ask for. E.g. if you ask for 12 bytes it may provide 16 (see A Memory Allocator, which notes that 16 is a common size). So it doesn’t need to know you asked for 12 bytes, just that it gave you a 16-byte chunk.

Leave a Comment