How does free know how many bytes to free?

Q

How does free know how many bytes to free?

✍: Guest

A

The malloc/free implementation remembers the size of each block as it is allocated, so it is not necessary to remind it of the size when freeing. (Typically, the size is stored adjacent to the allocated block, which is why things usually break badly if the bounds of the allocated block are even slightly overstepped.

2016-03-24, 333👍, 0💬