Sign in to follow this  

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

This topic is 2388 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 2388 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this