CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009
    Location
    Netherlands
    Posts
    91

    [RESOLVED] Multidimensional Array

    I´m messing around a bit with procedurally generating terrain for a game I´m working on. The terrain itself is simply represented by a 2 dimensional array which containts height values, basicaly a heightmap.

    The problem for me is, how do I implement this array. The array will be accessed and written to thousands of times in several loops and algorithms.

    The test code I had written used a simple 2 dimensional array (float[][]). But I need a way to determine the size of the array in the constructor of the Terrain object, and this isn't possible when I have to determine the size of the array when I declare it.

    So I tried a vector<vector<float>> setup. I have always used vectors over arrays in my code, and they have never cost me much performance wise. But when I used it here it slowed things down noticably. Perhaps it is because I have never extensivly accessed a vector of vectors before like I am trying now.

    At the moment I have another solution and my situation looks a bit like below (psuedocode):

    Code:
    Class Terrain
    {
    	float* terrainArray;
    	Terrain(int lenght, int width)
    	{
    		terrainArray = new float[lenght*width];
    	}
    
    };
    This is currently working fine for me. But I don't think its the best solution, because everytime I access or write to the array, I need to do it like so terrainArray[lenght*width]. Personally I would like to see stuff like terrainArray[lenght][width] or even terrainArray.set(lenght, width, value). Perhaps I am nitpicking and I should simply keep using the current implementation. But there are some other options that I am considering and I would like some second opinions on the matter.

    The current options I'm considering:

    - Keep using float* terrainArray = new float[lenght*width]
    - Write a thin wrapper class (called Array2D) around float* terrainArray = new float[lenght*width] to add some syntactic sugar.
    - Use a well known library such as boost::multiarray (I don't know about its performance versus a vector or my personal solution)

    Keep it constructive please, I don't consider myself a C++ veteran or anything. Do feel free to suggest other options as well.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Multidimensional Array

    Write a subscript operator and abstract away the 1 dimension into 2.

    http://www.parashift.com/c++-faq-lit...html#faq-13.10
    (this is only 90% on topic, but contains the gist of it)

    Once you write operator()(int i, int j), then you terrain is a 2D array for all intents and purposes.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Multidimensional Array

    Take a look at the following FAQ that provides a two-dimensional array including random access via the subscript operator ([]).

    Performance-wise it should pretty much match your C implementation if used correctly. Keep in mind that 'vector' allocates memory as it goes which certainly can hurt performance. You can use 'reserve' to alleviate this issue though.

  4. #4
    Join Date
    Apr 2009
    Location
    Netherlands
    Posts
    91

    Re: [RESOLVED] Multidimensional Array

    Thanks for the advice guys, you both pretty much suggest the same thing, which is what I ended up going with. It seems fine performance wise now, even if I do something ridicilous like make the array 20000x20000.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured