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

后端 未结 11 1009
执笔经年
执笔经年 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条回答
  •  囚心锁ツ
    2020-12-08 04:50

    There is always testing...

    You can try a "sample" program that allocates memory in a loop. This way you can see if your compiler magically allocates memory in powers of 2. With that information, you can try to allocate the same amount of total memory using the 2 strategies: random sized blocks and power of 2 sized blocks.

    I would only expect differences, if any, for large amounts of memory though.

提交回复
热议问题