Multi-dimensional array of struct problem.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Multi-dimensional array of struct problem.

Hybrid View

  1. #1
    Join Date
    Oct 2010
    Posts
    2

    Question Multi-dimensional array of struct problem.

    Hey guys, I'm having a problem with a 2 dimensional array of structs. Whenever I assign a value to one particular struct it appears that it contaminates that value for every other struct on that row.

    Here are the relevant lines of code.
    Code:
    struct Cell
    {
    	D3DXVECTOR2 position;
    	bool isSouthOpen,
    		 isEastOpen,
    		 isNorthOpen,
    		 isWestOpen,
    		 isActive;
    };
    
    ...
    
    Cell* cells;
    
    ...
    
    cells = new Cell[cellColumnSize, cellRowSize];
    
    for (int i = 0; i < cellColumnSize; ++i)
    	for (int j = 0; j < cellRowSize; ++j)
    	{
    		cells[i,j].position = D3DXVECTOR2(i, j);
    		cells[i,j].isSouthOpen = false;
    		cells[i,j].isWestOpen = false;
    		cells[i,j].isNorthOpen = false;
    		cells[i,j].isEastOpen = false;
    		cells[i,j].isActive = false;
    	}
    
    ...
    
    startCell = D3DXVECTOR2(0, 0);
    cellPointer = startCell;
    cells[(UINT) cellPointer.x, (UINT) cellPointer.y].isActive = true; // Sets the entire row to active for some reason.
    It's for a little game that procedurally generates a random maze. I've only got it rendering active cells so the output and watching the pointer's referenced values verifies that an entire row gets contaminated.

    I've never seen behavior like this so I imagine it might be due to some c++ behavior that I'm unaware of. Haven't been using c++ for very long.

    Thanks!

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Multi-dimensional array of struct problem.

    This does not give you a two dimensional array:
    Code:
    cells = new Cell[cellColumnSize, cellRowSize];
    You either need to make cells a pointer to a pointer of Cell, or, just use a vector of vectors:
    Code:
    std::vector<std::vector<Cell> > cells;
    Viggy

  3. #3
    Join Date
    Oct 2010
    Posts
    2

    Exclamation Re: Multi-dimensional array of struct problem.

    DOH!

    There goes my C# syntax habits kicking in.

    Thanks!!

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center