How does free know how much to free?

后端 未结 11 2132
情话喂你
情话喂你 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 03:53

    The heap manager stored the amount of memory belonging to the allocated block somewhere when you called malloc.

    I never implemented one myself, but I guess the memory right in front of the allocated block might contain the meta information.

提交回复
热议问题