0

If a 2d array is allocated memory in this fashion, how should it be deleted?

MyObject **lastJobs, **lastMachines ;
lastJobs = new MyObject*[getNbJobs()] ;
lastMachines = new MyObject*[getGreatestMachineNb()] ;

does this suffice:

delete[] lastJobs;
delete[] lastMachines;

2
Contributors
4
Replies
5
Views
12 Years
Discussion Span
Last Post by littletimmy
0

Thanks Dave,
this is code which I am tryig to understand however. In the above example while it is declared as a 2-d array it is only allocated memory in one direction. Does this make a difference. To be more clear, should I delete it as 1d or 2d array?

Thanks
P.

0

You delete what you new, and you delete[] what you new[].

In what you posted, I see what might be an attempt to allocate two 2-dimensional arrays, but only one dimension appears to be allocated for each. The second dimension of each 2-dimensional array is not yet allocated. So I believe these "rows" are each deallocated correctly in the code you've shown, but the "columns" were never allocated nor deallocated.

0

Thats great again Dave!
One last question (open to all-takers!!): If memory is allocated in the copy constructor using new must it also be deleted somewhere? The copy sonstructor isn't a special case for some reason!?!?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.