[VC++] When should I used the new keyword

Started by
2 comments, last by EmperorXYZ 12 years, 10 months ago
I've always been confused with this. I mean when should I used MyClass *obj = new MyClass(args) instead of MyClass obj(args)? You can probably explain this to me well, but I'd like to have general guidelines and examples. For now, I've always used new to create instances and it has been working, so I could afford being shamefully ignorant on this subject.
Advertisement
The difference is basically whether the object will use dynamically allocated memory (a.k.a. "from the heap"), or if it will use memory "from the stack" like all local variables and arguments normally use.

Most of the time you want to use local variables (without `new'), which will be destroyed automatically whenever they go out of scope. If you want your object to survive beyond that (for instance because you want to return a pointer to the object from the function where it's created), you may want to use `new'. Remember that every time you use `new' someone somewhere needs to use `delete' to destroy the object and release the memory eventually. You basically have to be very aware of (and hopefully document) what part of the code "owns" the object and is therefore responsible for cleaning it up. This is a common source of bugs (memory leaks), so I try not to use `new' in the first place, to avoid the trouble.

In certain circumstances I really need to use `new', for instance because I want to implement a factory (a function that can create objects of classes derived from some common base, and which returns a pointer to the base), but in those cases I tend to use a shared pointer to make sure that the object will be destroyed when nobody is using it.

Another reason to use dynamically allocated memory used to be to implement containers (lists, vectors, maps...), but you can almost always use the containers from the standard library and not have to deal with allocating and releasing memory yourself. You want to use containers of objects (`std::vector<Vertex>'), not containers of pointers (`std::vector<Vertex *>'), unless there is a good reason why you can't do that. The most common legitimate reason to not use a container of objects is that you want some sort of polymorphic behavior; you can probably use one of the Boost vector containers for that, or even a container of shared pointers.

Perhaps some fancy OO approaches have needs beyond what I just described. But if you can stick to simple usage of dynamic memory, I highly recommend it: Object ownership is usually obvious in my programs, and I haven't had a memory leak (or any other resource not properly released) in about a decade.
Use it only when you need to(like the examples Alvaro gave). Generally speaking, If you can achieve the same thing without using the new operator, don't use it.
I see. Well thanks. Seems rather simple. Only one thing to check to decide which to use. I've always thought choosing which needed some black sorcery to make clear.

This topic is closed to new replies.

Advertisement