for (i = 0; i < length; i++)
{
for (j = 0; j < width; j++)
{
for (k = 0; k < height; k++)
{
interpolated_1D[i* j *height + k] = interpolated_input[j][k] ;
}
}
}


Doesn't seem to work,,, What am I doing wrong... Everything is allocated fine. No seg faults.

thankls in advance
joe

Recommended Answers

All 9 Replies

The problem lies in the formula you're using to calculate the 1 dimensional array. It should be:

i*length*width + j*width + k

Since i is represents not only a row but also a column, you're going to have to multiply it by both to get a correct index value.

If you think about it, any multi-demensional array is in essence a 1D array..

If you have ever studied assembly language you would know this is true. My opinion would be to just keep your 3D array, and access everything through a 1D array subscript.

You could access every element allocated by this 3D array with just one subscript.. which in this case would be equal to i*j*k. Even though you may have originally allocated let's say, 5 elements for your first dimension, you could safely run out of bound of the first dimension because the memory allocated is contiguous. Example, if i=5, j=10, and k=20, you could safely traverse the 1D subscript up to 1000 elements.

You can safely overrun the first dimension of a multi-dimensional array to a value that is the product of all dimensions of that array in order to get 1D performance from the multi-dimensional array. This will save you code and overhead trying to unnecessarily copy a multi-dimensional array to a 1D array.

///////////////////////////"You can safely overrun the first dimension of a multi-dimensional array to a value that is the product of all dimensions of that array in order to get 1D performance from the multi-dimensional array. This will save you code and overhead trying to unnecessarily copy a multi-dimensional array to a 1D array."//////////////////


Could you explain more w/ code?


Thanks for the quick replies. I prefer the 3D array because I am doing a rotational matrix. However, VTK (a visualization library) requires a 1D array in order to make use of it.

Also, I am working with 2 full 3D 512*512*512 shorts, so memory is an issue, so I would like to avoid extra allocation espcially when both arrays are indentical. Is there a way to use pointers and not reallocate a new 1d array and just keep the allocated 3D array.

--In other words.. can I make a 1d array from my 3d without extra memory?

VTK needs a short *.... I have a short***

> --In other words.. can I make a 1d array from my 3d without extra memory?
Read my previous post. I *think* it works...

However, it's not very fast as explained by Clinton. Your choice: memory or speed. Generally in game programming it's memory since people buy lots of memory to run games anyway.

Here is a small example:

//Declare a 3D array
int myarray[5][10][20];

//Populate the array
for(int i=0; i<5; i++)
    for( int j=0; j<10; j++)
        for(int k=0; k<20; k++)

            myarray[i][j][k] = k;

//Display the array multi-dimensionally
for(int i=0; i<5; i++)
    for( int j=0; j<10; j++)
        for(int k=0; k<20; k++)

            cout << myarray[i][j][k] << endl;

//Display the array in one dimension
for(int i=0; i<1000; i++)

    cout << myarray[i] << endl;

Although it can be done this way, doesn't mean it should be done this way. Some would argue that this type of coding would be dancing barefoot on the broken glass of undefined behavior. However, my argument is, 'why try to make a 1D array out of a 3D array.. when a multi-dimensional array is already a 1D array...'

That is pretty interesting, I never knew that. However I don't think that will help me. I have a *** short pointer.

I am trying to fulfill
SetImportVoidPointer (void *ptr)

I tried force-casting it for shits and giggles and got undesirable results...

Is there a way I can form a new *short pointer 1D array, with no memory allocation and just have it point to the 3D array

I don't know if my computer is just being finnicky or what, but Clinton's code didn't work right for me. Here's some code and output (trivial changes to the code):

int main()
{
  //Declare a 3D array
  int myarray[5][10][20];

  //Populate the array
  for(int i=0; i<5; i++)
    for( int j=0; j<10; j++)
      for(int k=0; k<20; k++)

        myarray[i][j][k] = k;

  //Display the array multi-dimensionally
  for(int i=0; i<5; i++)
    for( int j=0; j<10; j++)
      for(int k=0; k<20; k++)

        cout << "multi: " << myarray[i][j][k] << endl;

  //Display the array in one dimension
  for(int i=0; i<1000; i++)
    cout << "single: " << myarray[i] << endl; 
}

Output:

multi: 0
multi: 1
multi: 2
multi: 3
multi: 4
...
multi: 16
multi: 17
multi: 18
multi: 19
single: 0xbfbbf300
single: 0xbfbbf620
single: 0xbfbbf940
...
single: 0xbfc81ea0
single: 0xbfc821c0
single: 0xbfc824e0

When I used this code, I got matching outputs for single and multi though:

int main()
{
  ... // same as before

  //Display the array in one dimension
  for(int i=0; i<1000; i++)
    cout << "single: " << myarray[0][0][i] << endl;

}

it also worked with (**myarray)[i] But like Clinton said, you don't want to mess with undefined behavior...

error on my part w/ overrunning the first dimension of the array instead of the last dimension.

The residual hex values are cause i think because of 'int' overflow.. here is a similar (tested) code:

#include<iostream>
using namespace std;

int main()
{
//Declare a 3D array
int myarray[2][4][6];

//Populate the array
for(int i=0; i<2; i++)
    for( int j=0; j<4; j++)
        for(int k=0; k<6; k++)

            myarray[i][j][k] = k;

//Display the array multi-dimensionally
for(int i=0; i<2; i++)
    for( int j=0; j<4; j++)
        for(int k=0; k<6; k++)

            cout << myarray[i][j][k] << endl;
            
cin.get();

//Traverse the entire array in just one subscript, similar to a 1D array
for(int i=0; i<48; i++)

    cout << myarray[0][0][i] << endl;
    
cin.get();
    
return 0;
}

I know this is frowned upon but can I do:

short ***3d;
short *1d

3d allocation//////////////and fill./////

1d = (short*)3d;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.