Dynamic three-dimensional array of pointers?

Jan 18, 2010 at 2:58pm
Alright. I've been programming a game in C++ for a while now, and I can not find the solution to this. I want to declare an array with three dimensions using the new keyword, since its dimensions will vary from each object. Also, the elements in the array are pointers to SDL_Surfaces. I have not yet found a way to do this. Any help is appreciated.
Jan 18, 2010 at 3:40pm
you should not do this, multidimensional dynamically allocated arrays is VERY unsafe, and hard to manage!
Jan 18, 2010 at 4:23pm
Jan 18, 2010 at 5:10pm
FYI, I did search. I only make forum posts as last resorts.

So that is the only way to allocate a dynamic array of pointers? If there is a better way, please tell me before I go and do something stupid. But I really can't see any other way to do this.
Jan 18, 2010 at 5:44pm
To my knowledge, yes that is the only way. Are you sure you need a 3D array though? Can you do it via 2D array with a container class? or perhaps Map of lists?

I guess the question is what are you trying to accomplish and is 3d array the only option?
Jan 18, 2010 at 5:52pm
No, this is the only way to allocate 3d arrays. What you can do is to use a single dimensional array, and think about it as a continuously allocated 3d array.
1
2
3
4
//To allocate
int *3darray = new int[width*height*depth];
//To access elements:
3darray[z*depth+y*width+x]; //I hope it's correct 

Alternatively use boost::multi_array
http://www.boost.org/doc/libs/1_40_0/libs/multi_array/doc/user.html
Last edited on Jan 18, 2010 at 5:52pm
Jan 18, 2010 at 5:56pm
Actually, that does give me an idea. One of the array dimensions is constant, so ill use that as a "standard" array, and just have 2D arrays within those elements (which is what I was aiming for in the first place, except I won't use the heap for the first dimension). I do know that a linked list would be horribly inefficient. I think I'll be able to handle this way just fine, if I use the destructor correctly.

@R0mai:
I considered this, but I didn't want to confuse myself when trying to access arrays. The initialization and destruction might be simpler, but I don't want to have to refer to its elements that way.

I think I can mark this thread as solved now. Thanks for the help.
Jan 18, 2010 at 6:22pm
You can easily make a wrapper class around it:
a simple example: (didn't compile it, usage should be obvious)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template<class T>
class array3d {
public:
    array3d(unsigned width, unsigned height, unsigned depth)
    : width(width), height(height), depth(depth), data(new T[width*height*depth]) {}

    T& operator()(unsigned x, unsigned y, unsigned z) { return data[z*depth+y*width+x]; }
    const T& operator()(unsigned x, unsigned y, unsigned z) const { return data[z*depth+y*width+x]; }

    const unsigned width, height, depth;
private:
    T *data;
    /*For simplicity leave these unimplemented*/
    array3d();
    array3d(const array3d&);
    array3d& operator=(const array3d&);
    
};
Last edited on Jan 18, 2010 at 6:22pm
Jan 18, 2010 at 7:56pm
I'm late to the game here, but here's an obligatory link:

http://www.cplusplus.com/forum/articles/17108/


Pretty much, I recommend what R0mai is suggesting.
Topic archived. No new replies allowed.