• Advertisement

Archived

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

Question about arrays

This topic is 5409 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
Check out this old thread.

Among other things, you''ll find a template class (written by yours truly) that allows you to create a 2D array out of a regular pointer.

Share this post


Link to post
Share on other sites
OK,I understand how I can create one now,but when I delete it,can I do

delete a;(a is a 2d array)?

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
quote:
Original post by jimywang
How can i implement a two dimension array in C++?

That question is answered in the C++ FAQ Lite, in section 16.

Share this post


Link to post
Share on other sites
You could simply create an array with just one dimension instead of two or more. Let''s say you have a 200x300 array ([200][300]), you could access, say, [100][50] via...

MyArray[(200*50)+100]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
is there any speed benefit from having an array of arrays over a 2d array?

ie: *(ptr+j) instead of arr[i][j]

Share this post


Link to post
Share on other sites

  • Advertisement