|
-
June 23rd, 2002, 04:45 AM
#1
ragged arrays
Hi,
I have to allocate memory for a 2D ragged array and I thought I had it figured out. But then I discovered that the following code causes a memory leak:
// Allocate memory for a 2D-array with varying row length
int no_of_rows = 10;
int* no_of_cols = new int[no_of_rows];
for(int i=0; i<no_of_rows; i++)
infilestream >> no_of_cols[i];
CMyClass** myO = new CMyClass*[no_of_rows];
for(i=0; i<no_of_rows; i++)
myO[i] = new CMyClass[no_of_cols[i]];
... code using myO[i][j] ...
// Deallocate the memory
for(i=0; i<no_of_rows; i++)
delete [] myO[i];
delete [] myO;
delete [] no_of_cols;
The problem is with the deallocation of the array. But I don't know how to do it differently. Any hint on a solution would be
greatly appreciated.
Regards, Ralf
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|