If the Space you have to allocate must be contiguous it has to be allocated with a single 'new' otherwise the memory will not be contiguous.
This would look like this:
int d1 = 10; // first
int d2 = 10; // second
int d3 = 10; // third dimension
int* array3D = new int[d1 * d2 * d3];
with this you have allocated enoug space for your 3D array, now this has to be mapped to 3D.
array3D[(1*d1*d2) + (2*d2) + (3)]; // access element at 1,2,3
With this you can Map every point of this 1D array that you Allocated onto a unique point in 3D space.
As you might see this is very error prone. So you should not ever do this like that.
Dont ever use new/delete to allocate an array like this:
use std:array
or std::vector
to handle this for you.
The use of raw new/delete leads to errors, if anything was allocated with new and you forget to delete it, or you overlook something, there will be a memory leak.
void test(){
int* a = new int[20];
// do stuff with a...
if(error)
return; // oops this is a leak
delete a; // only executed if there was no error,
}
std::array
is to be used if you know how big the array has to be at compile time, and it never has to change.
std::vector
on the other hand can be used if you dont know the size at compile time, it can change while your programm is running.
std::array<int, 10> test1; // creates a fixed size array of size 10 and type int.
std::vector<int> test2(10); // creates an array that can change at runtime:
test2.push_back(2); // the vector now has size 11 and the last element is equal to 2
This way you also don't have to delete
the array at the end.
If you want to be able to use this more often in your code, it can be very helpful to wrap all this functionality in a class:
#include <array>
template<typename T, std::size_t _D1, std::size_t _D2, std::size_t _D3>
class Array3D{
std::array<T, _D1*_D2*_D3> elements;
public:
std::size_t D1(){ return _D1; }
std::size_t D2(){ return _D1; }
std::size_t D3(){ return _D1; }
T& element(std::size_t d1, std::size_t d2, std::size_t d3){
return elements[(d1*_D1*_D2) + (d2*_D2) + (d3)];
}
};
int main(){ // argc/argv not required if you dont use them
Array3D<int, 10, 10, 10> array;
array.element(1,2,3) = 5;
// loop thorug all elements
// the methods d1,d2,d3 return the dimensions you gave them initialy
// this way if you cange the array size you dont have to change this loop at all
for(std::size_t i = 0; i < array.D1(); i++)
for(std::size_t j = 0; j < array.D2(); j++)
for(std::size_t k = 0; k < array.D3(); k++)
array.element(i,j,k) = 5;
// no delete
}