Thursday, June 14, 2007

use the same form in corresponding uses of new and delete

match every new with the right delete.

new -> allocate memory + call constructor on that memory.
delete -> call destructor + deallocate memory

the big question for delete is how many objects reside in the memory being deleted . that determines how many destructor's wud be called.


memory layout for single objects is different from memory layout for arrays of objects.
fore.g. one way is
single objects : ob
array : n , obj , obj ...

compilers can implement in differently
dependign on the version of delete called it knows whether to delete an array or delete an object.

remember it when you have multiple constructors, in that case use consistently same new to allocate memory so that you know what version of delete to call in memory.

also be careful with typedef's as the author needs to define which delete shud be used with it.
typedef std::string addresslines[4];
std::string *pal = new AddressLines;

delete pal;
delete[] pal;


things to remember:
if you use [] in a new expression , you must use [] in the corresponding delete expression. if you dont use [] in a new expression , you mustnt use [] in the corresponding delete expression

No comments: