1

可能重复:
C++ 的新操作是否保证地址返回对齐?

在这个程序中,我打印了new返回的每个无符号字符的地址。然后最后将它们向后删除。

#include "stdafx.h"
#include<stdlib.h>
void func();

int main()
{
    int i=10;
    while(i-->0)printf("loaded %i \n", (new unsigned char));
    getchar();
    unsigned char *p=new unsigned char;printf("last pointer loaded %i \n", p);
    i=10;
    while(i-->0)delete (p-=64);
    getchar();
    p+=640;
    delete p;//nearly forgot to delete this ^^
    return 0;
}

输出:

在此处输入图像描述

如您所见,每个new返回 64 字节对齐的数据。

问题:这个 64 字节等于缓存行大小还是只是一个编译器的东西?

问题:我应该让我的结构大多为 64 字节长吗?

问题:当我更改我的 cpu、ram、操作系统或编译器时,这会有所不同吗?

奔腾-m, VC++ 2010 express, windows-xp

谢谢。

4

1 回答 1

2

The implementation choices for a heap manager make a lot more sense when you consider what happens after a large number of allocations and deallocations.

A call to malloc() needs to locate a block of unused block of sufficient size to allocate. It could be bigger (in which case, it could either create a free block with the difference - or waste it). A naive strategy of finding the closest size of block is called best fit. If it goes onto to create new free blocks, you could alternatively call it worst leave.

After use, the best-fit approach results in a large amounts of fragmentation, caused by small blocks that are unlikely to be ever allocated again, and the cost of searching the free blocks becomes high.

Consequently, high performance heap managers don't work like this. Instead they operate as pool allocators for various fixed block-sizes. Schemes in which the blocks are powers of 2 (e.g. 64,128,256,512...) the norm, although throwing in some intermediates is probably worthwhile too (e.g. 48,96,192...). In this scheme, malloc() and free() are both O(1) operations, and the critical sections in allocation are minimal - potentially per pool - which gets important in a multi-threaded environment.

The wasting of memory in small allocations is a much lesser evil than fragmentation, O(n) alloc\dealloc complexity and poor MT performance.

缓存行大小的最小块大小是那些经典的工程权衡之一,可以肯定的是,微软做了很多实验来达到64他们的最小值。FWIW,我很确定您会发现现代 CPU 的缓存线大小比这更大。

于 2012-08-10T22:20:28.080 回答