Skip to main content
GameDev.net gamedev.net
🔒 Locked

2/3D array in C++

Started by Dwiel Jan 9, 2004 at 5:09 PM 4 replies 700+ views
Original Post
Dwiel
Dwiel
Hello, I am working on the implementation of perlin noise in my 3D RTS game, and was wondering what the preffered method for creating multidimensional arrays is... I noramlly either use vector inside a vector or nested new calls... I am wondering if the nested new call approach is bad. Wont that be fraging my memory pretty bad? This array is going to be acting as a cache, so it will be accesed often and changed every once in a while... read anywhere from every second to every minute... I was thinking of just using a 1D array and accessing it like a 2D array. Is this the preffered method for this kind of application? Thank you very much. Dwiel [EDIT] got rid of some irrelivent info... [edited by - Tazzel3d on January 9, 2004 6:34:18 PM]
Megahertz
Megahertz
Personally, I just use a 1D array and do the indexing myself, but thats just me. Others do it differently. Just whatever works for the individual I suppose.

-=[ Megahertz ]=-
-=[Megahertz]=-
Dwiel
Dwiel
I guess I was just wondering if there were any technical benifits from doing it one way or another

Thanks

Dwiel
daerid
daerid
Depends on what your needs are. If you''re worried about memory fragmentation, and can spare a few CPU cycles, go with the single dimension array and calculate the indices yourself. Otherwise, if you''re working in a fragmentation-reducing memory managed environment (e.g: a game w/ a custom memory manager), then nested vectors would probably be your best and easiest bet.


daerid | Legends | Garage Games | Spirit | Hapy | Boost | Python | Google
"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool
daerid@gmail.com
SiCrane
SiCrane
I tend to use an multidimensional array class, which is usually a wrapper around a one dimensional vector. As I see it, using a single dimensional array has the advantages of better locality of access, less fragmentation and better behaviour under some compiler optimizations, especially inside of loops.

I sometimes use hashtables or std::maps indexed off of std::pairs (for sparse two dimensional arrays).

I can only think of a handful of situtations where I'd use a vector of vectors, and those are when I actually need multiple vectors instead of a multidimensional array.

And I can think of no situation where I'd use the nested new approach.

edit: #@!$ smiley code

[edited by - SiCrane on January 9, 2004 11:34:14 PM]
Dwiel
Dwiel
ok. thanks. It seemed like the array of pointers to 1D data was kinda bad... Also, isnt it faster to access memory from RAM that is all close together instead of jumping around as you might be when you use my old method? I guess I''l prolly just use the single approach. Plus, that way eliminates the large number of calls to new and delete, which as I havefound are not particulary fast...

thanks again

Dwiel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.