Re: Always avoid "new"
From: Niels Dybdahl (ndy_at_fjern.detteesko-graphics.com)
Date: 04/30/04
- Next message: Dave Moore: "Re: template & void argument"
- Previous message: Rob Williscroft: "Re: Program to rename C/C++ symbols (variables/functions)"
- In reply to: seesaw: "Always avoid "new""
- Next in thread: DaKoadMunky: "Re: Always avoid "new""
- Reply: DaKoadMunky: "Re: Always avoid "new""
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 30 Apr 2004 11:26:34 +0200
> 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
- Next message: Dave Moore: "Re: template & void argument"
- Previous message: Rob Williscroft: "Re: Program to rename C/C++ symbols (variables/functions)"
- In reply to: seesaw: "Always avoid "new""
- Next in thread: DaKoadMunky: "Re: Always avoid "new""
- Reply: DaKoadMunky: "Re: Always avoid "new""
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|