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.
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 = newint[128]; // (2x64)
int** 2d = newint[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.
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.