2

My current code is:

a = new int[10];

and the address of a is 0x...040 I want it to be 4096 byte aligned so I tried to change it to:

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

but still this doesn't work (the address is still ends with 040 instead of with 000. What am I doing wrong?

1 Answer 1

7

__declspec(align(...)) can be used for static arrays, like:

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

For dynamic allocation use _aligned_malloc function, use _aligned_free to release array allocated by _aligned_malloc:

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

Required include is malloc.h

1
  • error "_aligned_malloc" id undefined ? yes I have included the header
    – Ajay
    Feb 5, 2015 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.