Archived

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

jimywang

Question about arrays

Recommended Posts

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
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