what happens when tried to free memory allocated by heap manager, which allocates more than asked for?

后端 未结 5 2153
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-15 10:45

This question was asked to me in an interview.

Suppose char *p=malloc(n) assigns more than n,say N bytes of memory are allocated and free(p) is used to free the memory a

5条回答
  •  無奈伤痛
    2021-02-15 11:00

    Generally the heap manager will free whatever it allocated. It stores this info somewhere, and looks it up when free() is called.

    A heap manager is not "faulty" if it allocates more memory than was requested. Heap managers often work with fixed block sizes, and will round up to the next appropriate block size when satisfying a request. The heap manager's job is to be as efficient as possible, and often big efficiencies result from a few small inefficiencies.

提交回复
热议问题