Is it right thing to always avoid using "new" to create objects? What if
after starting the application, then decide which and how many objects to
create? (Seems like under such situation is there no other choice but using
"new")
I do actually avoid "new" as much as possible. The reason is this: The
destructor and deallocator are not called automatically, so to prevent
memory leaks other methods are needed such as try, catch or destructors from
other objects.
Of course if I need a variable number of objects, then I use new and store
the objects in a vector and let the vector do the cleanup. Or in those cases
where I do not store the object(s) in a vector, I store them in some other
object and make that objects destructor ensure cleanup.
An additional advantage of allocating on the stack instead of the heap, is
that if you have tested your application and thereby verified that there is
enough space on the stack, then you can be sure that there will always be
enough space on the stack for your objects (unless you depend on recursion).
With the heap you can not be sure that there is enough space and you risk
exceptions on that account.
Niels Dybdahl