2

我目前的代码是:

a = new int[10];

并且地址a0x...040我希望它是4096字节对齐的,所以我尝试将其更改为:

a = new __declspec(align(4096)) int[10];

但这仍然不起作用(地址仍然以040而不是结尾000。我做错了什么?

4

1 回答 1

7

__declspec(align(...))可用于静态数组,例如:

__declspec(align(4096)) int a[10];

对于动态分配使用_aligned_malloc功能,用于_aligned_free释放分配的数组_aligned_malloc

int* a = (int*) _aligned_malloc(10 * sizeof(int), 4096);
...
_aligned_free(a);

必需的包括是 malloc.h

于 2013-10-01T14:56:51.127 回答