freeing memory allocated in hierarchy of structs

If I dynamically allocate a struct (using operator new) and then allocate array of structs inside it, and in those struct allocate more structs creating a hierarchy of 3 levels of dynamically allocated structs, how does one free the memory?

Do I need to keep track of each struct array that has been allocated on each level of hierarchy or just free the top and the the ones furhter down shall be freed?
Do I need to keep track of each struct array that has been allocated on each level of hierarchy or just free the top and the the ones furhter down shall be freed?


You must delete all the allocated memory, not just the top. You will need to delete the memory in the reverse order of the calls to new.

The better alternative would probably be the use of a standard container instead of dynamically allocating the memory, or consider the use of smart pointers instead of the "raw" pointers.


Topic archived. No new replies allowed.