| | |
3d to 1d Array Conversion
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
The problem lies in the formula you're using to calculate the 1 dimensional array. It should be:
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.
C++ Syntax (Toggle Plain Text)
i*length*width + j*width + k
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
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.
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.
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 0
///////////////////////////"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***
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***
Last edited by Joe689; Feb 19th, 2007 at 7:43 pm.
> --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.
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.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
Here is a small example:
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...'
C++ Syntax (Toggle Plain Text)
//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...'
Last edited by Clinton Portis; Feb 19th, 2007 at 8:09 pm.
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 0
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 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):
Output: When I used this code, I got matching outputs for single and multi though:
it also worked with
But like Clinton said, you don't want to mess with undefined behavior...
C++ Syntax (Toggle Plain Text)
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; }
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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; }
(**myarray)[i]But like Clinton said, you don't want to mess with undefined behavior...
Last edited by Infarction; Feb 19th, 2007 at 10:21 pm.
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:
The residual hex values are cause i think because of 'int' overflow.. here is a similar (tested) code:
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Clinton Portis; Feb 20th, 2007 at 12:05 am.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: help with writing each 100 lines into different files.
- Next Thread: Swapping chars within an array
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






