1

If I had an array defined as follows in C:

int B[d1][d2][d3][d4][d5][d6][d7][d8][d9];

Then it is easy to convert "B" into single dimensional one. But what if I have an array in C, which is defined as follows:

int********* A;
//which will be allocated as follows for example
A = (int*********) malloc(N*sizeof(int********));
for(int i=0; i<N; i++)
{
 //differentSizes will be distinct for every "i"
 //the same distinctness of sizes will be for every other 
 //inner parts of all inner for loops
 compute(&differentSizes);
 A[i] = (int********) malloc(differentSizes*sizeof(int*******));
 for(...)
 {
  ...
 }
}

Where "A" will have very large sizes for every dimension and they will all be distinct for all the innerarrays/subarrays of "A".

My question: Is there an efficient way to convert "A" into a single dimensional one? If possible can you show a simple example? Thanks!

starter
  • 325
  • 1
  • 5
  • 15
  • What is `int[d1][d2][d3][d4][d5][d6][d7][d8][d9] B;`? Did you mean: `int B[d1][d2][d3][d4][d5][d6][d7][d8][d9];`? – Mike Oct 17 '12 at 15:58
  • @Mike Sorry, changed, thanks! – starter Oct 17 '12 at 15:59
  • Is your question "How do I convert an array allocated like A into one like B?" – AShelly Oct 17 '12 at 16:12
  • Side note: Write `A = malloc(N*sizeof(*A));` instead of `A = (int*********) malloc(N*sizeof(int********));`. This way it would be more readable and much less error prone, not to mention shorter. – Shahbaz Oct 17 '12 at 16:19
  • @AShelly No, my question is how do I convert array like A into a single dimensional one. Thanks! – starter Oct 17 '12 at 16:20
  • @starter, after flattening the arrays, if they have different sizes in each dimension and for each element, how are you going to know which cell belongs to which coordinates in the array? Or that doesn't matter? – Shahbaz Oct 17 '12 at 16:21
  • @Shahbaz They matter. Thanks! – starter Oct 17 '12 at 16:23
  • Your `A` is not an array but a pointer to pointer to pointer... These are really different kind of animals. – Jens Gustedt Oct 17 '12 at 16:38
  • So for example if you have `int **a`, where `a[0]` is an `int[5]`, `a[1]` is an `int[3]` and `a[2]` is an `int[7]`, how are you planning on understanding what original `a[x][y]` a given `flattened_a[z]` corresponds to? – Shahbaz Oct 17 '12 at 17:16
  • @Shahbaz We can do it as follows: let's say a_01=5, a_11=2, and a_21=7. Then z=sum(a_i1)+y, where i runs from 0 to (x-1). But I am asking for an efficient way if other exists. Thanks! – starter Oct 17 '12 at 18:19
  • @Shahbaz Or at least efficient implementation of this approach. Thanks! – starter Oct 17 '12 at 18:55
  • @starter, with 9 dimensions, I'm pretty sure computing such a formula is very very difficult. Care to explain why you want to flatten the array? – Shahbaz Oct 17 '12 at 19:23
  • @Shahbaz Just wanted to know if it would be possible to do it efficiently... Thought this way it would be easier to do the computations. Thanks! – starter Oct 17 '12 at 21:53
  • @Shahbaz I may need that array on CUDA kernel also, thanks! – starter Oct 23 '12 at 17:49

2 Answers2

2

I don't see your problem. Multidimensional arrays are contigous in memory, so a type conversion will work:

int B[13][37];
int *oneDimension = (int *)B;

From now on, you can access the elements of B by computing the appropriate offset from its size in each dimension; using the example above:

int valueAtB_2_6 = oneDimension[2 * 13 + 6];
  • This is true for static/global/local arrays, but not for an array allocated on heap (if all its rows and dimensions are allocated separately, in a loop). – Dmytro Sirenko Oct 17 '12 at 16:02
  • 5
    @EarlGray Those types of arrays aren't created using `int B[dim1][dim2][dim3];`. –  Oct 17 '12 at 16:03
1

The way your array is declared all the contents will be contiguous in memory. If you could determine the number of elements you could just copy the array over to a single dimensional one or iterate over the original it depends on what you want to do:

int* singleIndex = (int*)B;

for (int i = 0; i < d1 * d2...dN; i++)
{
    printf("Element # %d = %d\n", i, *singleIndex);
}

for instance.

Now if you were doing heap initialization of the array then this wouldn't work since all the memory would be scattered around on the heap but for static/stack allocated arrays it would.

ThePosey
  • 2,734
  • 2
  • 19
  • 20