memory alignment

Jan 9, 2013 at 11:11pm
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 Jan 10, 2013 at 3:23pm
Jan 10, 2013 at 4:36pm
Could you elaborate the question a little? Memory allocated with new is already aligned.
Jan 15, 2013 at 1:21am
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];
Jan 18, 2013 at 9:42am
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?
Jan 18, 2013 at 1:46pm
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.