Instead of manually allocating an array you should at least use a std::vector
. What you would do is have a header file that contains
extern std::vector<std::vector<std::vector<int>>> data;
that you will include in all the cpp files you wish to share the vector with and then in a single cpp file have
std::vector<std::vector<std::vector<int>>> data = std::vector<std::vector<std::vector<int>(a, std::vector<std::vector<int>>(b, std::vector<int>(c)));
and now you will have a global object that is shared and it has a managed lifetime.
You shouldn't really use a nested vector though. It can scatter the data in memory so it isn't very cache friendly. You should use a class with a single dimension vector and pretend that it has multiple dimensions using math. A very basic example of that would look like
class matrix
{
std::vector<int> data;
int row; // this really isn't needed as data.size() will give you rows
int col;
int depth;
public:
matrix(int x, int y, int z) : data(x * y * z), row(x), col(y), depth(z) {}
int& operator()(int x, int y, int z) { return data[x + (y * col) + (z * col * depth)]; }
};
and then the header file would be
extern matrix data;
and a single cpp file would contain
matrix data(a, b, c);