Jump to content
  • Advertisement
Sign in to follow this  
MTclip

delete the mult-dim array ?

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK.. in short its a mult dimensional array that is dynamically created.. but when the code runs and the destructor gets called to delete the array ... it gets a run-time error --- access violation --- how should i solve this..

CGridPiece	**m_gridPieces;      

////////// CREATE  called through constructor/////
m_gridPieces = new CGridPiece*[m_gridPiecesAcross];
for (int i = 0; i < m_gridPiecesAcross; i++)
      m_gridPieces = new CGridPiece[m_gridPiecesDown];

///////// DELETE  called throug destructor/////
for (int i = 0; i < m_gridPiecesAcross; i++)	
     delete [] m_gridPieces[m_gridPiecesDown];
delete [] m_gridPieces;

}



thanks...

Share this post


Link to post
Share on other sites
Advertisement
ok i used...

delete [] m_gridPieces;

does that delete every entry?
most likely not... ;(

EDIT::: == NEVER MIND ==


for (int i = 0; i < m_gridPiecesAcross; i++)
delete [] m_gridPieces;
delete [] m_gridPieces;




sometimes i wonder if im just retarded..



Share this post


Link to post
Share on other sites
You have to delete the i'th entry in the inner loop instead.
edit: Too late..

Unless you want to modify the *structure* of the array (i.e. move rows around) I suggest allocating all of the entries as a single array. You can even allocate the inner array only and calculate the indicies manually on each request.
// allocation
m_gridPieces = new CGridPiece*[m_gridPiecesAcross];
CGridPiece *p = new CGridPiece[m_GridPiecesAcross * m_GridPiecesDown];

for (int i = 0; i < m_gridPiecesAcross; ++i) {
m_gridPieces = p;
p += m_gridPiecesDown;
}

// descrution
delete [] m_gridPieces[0];
delete [] m_gridPieces;
And before anyone suggest it.. Yes, you could use a std::vector of std::vector's but that's far from an efficient solution. Though I suppose you could easily switch the underlying storage of the single allocation version to a standard container if you must.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!