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;

Recommended Answers

All 4 Replies

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?


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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.