Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jimywang

Question about arrays

This topic is 5536 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

How can i implement a two dimension array in C++? and how can i delete it to free up the memory? Can someone help plz? thx.

Share this post


Link to post
Share on other sites
Advertisement
lets say you want a two dimensional array of int type...

int myArray[2][2];

that will give you a 2x2 array.

Share this post


Link to post
Share on other sites
Hi jimywang,
Are you looking for creating a 2-D pointer array or a 2-D static array?. If you don''t want to deal with pointers and know beforehand about the size of the data that you will be handling, then the solution is simple. rhouve has given the simplest example for you and you can build on that.

If you can reply about your interest in pointer based arrays, then someone here can post a response.

Cheers!!!
V.

Share this post


Link to post
Share on other sites
For dynamically allocated 2D arrays I usually do:


      
// I got a nice 2D array...

ppSomeArray = new char*[nWidth];
for(int i=0;i<nWidth;i++)
ppSomeArray[i] = new char[nHeight];



// ...but now I don't want it anymore!

for(int i=0;i<nWidth;i++)
delete [] ppSomeArray[i];
delete [] ppSomeArray;


Someone please correct me if there is a better way to do this.


Your opinion is wrong, have a nice day!

[edited by - Valderman on April 28, 2003 6:52:14 PM]

[edited by - Valderman on April 28, 2003 6:53:29 PM]

[edited by - Valderman on April 28, 2003 6:54:07 PM]

Share this post


Link to post
Share on other sites
That would cause a memory leak.

Loop through the first array, deleting the arrays contained within individually, then delete the first array.

  
// ...but now I don''t want it anymore!

for(int i=0;i<nWidth;i++)
delete [] ppSomeArray[i];
delete [] ppSomeArray;



Your opinion is wrong, have a nice day!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!