Sign in to follow this  
chbrules

Creating Multi-Dimensional Array With Vars

Recommended Posts

chbrules    170
I've got a bit of an issue at hand. I have a game engine I'm working on and it's 2D overhead style. My map consist of an x and y dimension as well as multi-levels of a 3rd dimension of sorts. It's basically layered maps on top of one another. My issue is when I read in the values of the map's dimensions from my file format I want to create a 3D array to hold all the values instead of a slower Vector, but I come accross this issue: error C2540: non-constant expression as array bound
Map = new sMapBase[Header.layer_count][Header.height][Header.width];
Map is a pointer of type sMapBase. Maybe it's late and I'm doing something wrong, but I figured you could call dynamic memory and create a variable multidimensional array at any time? Thanks!

Share this post


Link to post
Share on other sites
Fukushuusha    186
You either have to use a 3d matrix (does such a thing exist? I guess it must, but I never had the need to use it), or a triple pointer: sMapBase map***

That is because, in arrays you have to give constants for values in all the fields except the first one.

Share this post


Link to post
Share on other sites
Fukushuusha    186
:( , note that I am not the most knowledgeable person you can find as far as C++ is concerned, so wait for other replies too, since some of them might be able to help you in other ways too.

Share this post


Link to post
Share on other sites
OrangyTang    1298
Its often easier to spoof multidimensional arrays by allocating a single 1d array and doing the indexing into it by hand. You can wrap that up in a class easily and the memory management is much easier and clearer IMHO. (Of couse, thats probably what Boost is doing anyway).

Share this post


Link to post
Share on other sites
chbrules    170
I'm just going to use one giant single dimensional array. It won't break the bounds of 2^32 (32-bit int for array length limits) since I'm restricting things anyway. Just have to use a little snazzy algebra to find the exact tiles I'll want to render to the screen. I figure that tiny overhead will save tons in comparison to what some large abstract data structure would do. (Like boost's multidimensional array or multidimensional vectors)

Speaking of that, can arrays be larger than a 32-bit integer on a modern x86-32 AMD64 CPU with the MSVS .NET 2003 compiler? I studied a bit about ASM and the x86 architecture, but that was from that archaic "The Art of Assembly" book.

Share this post


Link to post
Share on other sites
Sc4Freak    643
I don't think so. Not on a 32-bit platform, at least, because the memory pointers are 32-bit and thus restricted to about 4gb of addressable space. It's not really something for a program to be worrying about - unless you're going to use more than 4 gigabytes of memory.

64-bit can address up to 16 exabytes, IIRC.

Share this post


Link to post
Share on other sites
ZQJ    496
You won't even get particularly close to 2^32 in practice, because of the way the OS lays out virtual memory. Essentially that 4GB has to cover your program, the stack and a whole chunk is reserved for the OS itself to speed up kernel mode switches, so you might get 2GB in practice, you might not, I've never tried.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this