Dynamic creation of 3D Array
-
Hi all, How can i dynamically create a 3D array ? I have tried using new operator. But apparently it only support the dynamic creation of the first dimension.Can someone help me? Thanks Mahesh
-
Hi all, How can i dynamically create a 3D array ? I have tried using new operator. But apparently it only support the dynamic creation of the first dimension.Can someone help me? Thanks Mahesh
The short answer is because the syntax to do so is horrible, you can use much nicer tools to do it for you. I believe this will do what you want. Note the use of the delete[] operator to delete arrays.
= new int*[dy]; for(y = 0; y < dy; y++) { pMatrix[x][y] = new int[dz]; } } for(x = 0; x < dx; x++) { for(y = 0; y < dy; y++) { delete[] pMatrix[x][y]; } delete[] pMatrix[x]; } delete[] pMatrix;I think you'll agree that this is much nicer.vector<vector<vector<int> > > matrix; matrix[0][0][0] = 1;
However if you need to do alot of matrix math then I strongly suggest you look at something like boost::uBlas which uses template proxy classes to eliminate the expensive copies that are the price of using vectors.
If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling