C - Design your own free( ) function

前端 未结 8 1546
感动是毒
感动是毒 2020-12-29 08:27

Today, I appeared for an interview and the interviewer asked me this,

  1. Tell me the steps how will you design your own f
8条回答
  •  灰色年华
    2020-12-29 09:10

    Memory usage patterns could be a factor. A default implementation of free can't assume anything about how often you allocate/deallocate and what sizes you allocate when you do.

    For example, if you frequently allocate and deallocate objects that are of similar size, you could gain speed, memory efficiency, and reduced fragmentation by using a memory pool.

    EDIT: as sharptooth noted, only makes sense to design free and malloc together. So the first thing would be to figure out how malloc is implemented.

提交回复
热议问题