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

后端 未结 5 2137
忘了有多久
忘了有多久 2021-02-15 10:48

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:12

    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.

提交回复
热议问题