Creating Multi-Dimensional Array With Vars

Started by
9 comments, last by Zahlman 17 years, 1 month ago
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!
-Conrad
Advertisement
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.
Well, that sucks. Looks like I'm going to have to use an array of 2D vectors.
-Conrad
:( , 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.
Third person to ask (almost) the same question in three days :),

take a look at Boost.MultiArray, its prety much the easiest thing to use in this situation.
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).
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.
-Conrad
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.
NextWar: The Quest for Earth available now for Windows Phone 7.
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.
I won't come anywhere close to either, so no problem. Thanks guys! :D
-Conrad

This topic is closed to new replies.

Advertisement