Is it better to allocate memory in the power of two?

后端 未结 11 1045
执笔经年
执笔经年 2020-12-08 04:26

When we use malloc() to allocate memory, should we give the size which is in power of two? Or we just give the exact size that we need?
Like



        
11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 04:37

    It's somewhat irrelevant.

    Malloc actually allocates slightly more memory than you request, because it has it's own headers to deal with. Therefore the optimal storage is probably something like 4k-12 bytes... but that varies depending on the implementation.

    In any case, there is no reason for you to round up to more storage than you need as an optimization technique.

提交回复
热议问题