How does free know how much to free?

后端 未结 11 2467
情话喂你
情话喂你 2020-11-22 03:28

In C programming, you can pass any kind of pointer you like as an argument to free, how does it know the size of the allocated memory to free? Whenever I pass a pointer to s

11条回答
  •  佛祖请我去吃肉
    2020-11-22 04:09

    From the comp.lang.c FAQ list: How does free know how many bytes to free?

    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)

提交回复
热议问题