Simple rules to follow:
- for each allocation, there has to be a deallocation (ex1 is therefore wrong)
- what was allocated using
newshould be freed usingdelete, usingnew[]should be deallocated usingdelete[]and usingmallocshould be deallocated usingfree(ex3 is therefore wrong)
Conclusion, ex2 is OK.