• Advertisement

Archived

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

Cleaning up Resources?

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

i have this: (well, those arent the real data types, but i thought i would keep it simple) int **matrix; int rows; int cols; somewhere i do this: matrix = new int*[rows]; for(int i=0; i=new int[cols]; then, when i want to do cleanup, i tried this: for(int i=0; i

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
This should be how to properly create and destroy(minus error checking):



// Creation
matrix = new int*[rows];

for(int i=0; i < rows; i++)
matrix[i]=new int[cols];

// Destruction
for (int i=0; i < rows; i++)
delete [] matrix[i];

delete [] matrix;


Share this post


Link to post
Share on other sites
Can''t you just do this:

  

int **matrix;

matrix = new int [rows][cols];

matrix[y][x] = value;

delete [][] matrix;



I''m not using C++, do I won''t bother compiling it...
Instead of using a two-dimensional array, use a one-dimensional, linear array instead (faster too!):

  

int *matrix;

matrix = new int [rows * cols];

matrix[x + y * cols] = value;

delete [] matrix;



Didn''t compile this one either

Share this post


Link to post
Share on other sites

  • Advertisement