memory alignment

Is there any way to align memory allocated with new to a 2d array?. how can I aligned memory allocated to a 2d array or totally allocated to an object or array of objects.

Thanks
Last edited on
Could you elaborate the question a little? Memory allocated with new is already aligned.
You can ensure the memory is aligned by allocating it as a 1d array and then using pointers to reference different segments as a represenation of the 2D data you want to hold.

e.g
1
2
3
4
5
6
7
int* array = new int[128]; // (2x64)
int** 2d = new int[2];
2d[0] = array[0];
2d[1] = array[64]; // Uses double de-referencing though.

// Better to use
int *element = array[64 * x + y];
Is there any way to align memory allocated with new to a 2d array?. how can I aligned memory allocated to a 2d array or totally allocated to an object or array of objects.

Why are you worried about alignment?
If he was not worried about alignment, he could as well coded it in Java...
I always hear that the main killer feature of C++ is fine control over memory alignment and locality.
Topic archived. No new replies allowed.