-
December 1st, 2010, 07:40 AM
#1
C++ new operator
Greetings,
Can someone please explain difference between the following code:
Echo objName;
Echo objName = new Echo();
I don't understand when I must use line 1, or line 2.
Thank you.
-
December 1st, 2010, 08:01 AM
#2
Re: C++ new operator
As written, line 2 will not work. The operator new returns a pointer.
This will create an object of type Echo on the heap, but you will have no way of using it or deleting it to recover the memory.
Code:
Echo* objName = new Echo;
This will create an object of type Echo on the heap, and the pointer objName will point to it. The memory will not be recovered until you order it to be done, with
which means that as long as you don't lose the pointer (or a copy of the pointer), you will be able to use that object anywhere in your code, at any time.
If you create the object on the stack with
then you will not have to recover the memory yourself; it will be done when the object goes out of scope. This means that you cannot use the object from anywhere outside the scope.
Use new when you want the object to continue to exist outside the scope of where you create it. Do not lose the pointer to it (or a copy of that pointer), or you will lose the object and never be able to delete it.
-
December 1st, 2010, 08:49 AM
#3
Re: C++ new operator
Excellent, very well explained thank you!
-
December 1st, 2010, 11:40 AM
#4
Re: C++ new operator
Originally Posted by Moschops
then you will not have to recover the memory yourself; it will be done when the object goes out of scope. This means that you cannot use the object from anywhere outside the scope.
While this is true, note that it's referring to dynamic scope, not static scope. So if you call a function within the scope of the object and pass in a pointer or reference to the object, that's fine, you can still use the object in that function (though probably referring to it by a different name).
Use new when you want the object to continue to exist outside the scope of where you create it. Do not lose the pointer to it (or a copy of that pointer), or you will lose the object and never be able to delete it.
It is recommended to always use smart pointer classes for storing heap-allocated objects. This makes it much easier to deal with them properly.
-
December 1st, 2010, 12:17 PM
#5
Re: C++ new operator
Originally Posted by Lindley
It is recommended to always use smart pointer classes for storing heap-allocated objects.
Not by me
-
December 1st, 2010, 12:22 PM
#6
Re: C++ new operator
Any particular reason, or are you just masochistic?
-
December 1st, 2010, 12:23 PM
#7
Re: C++ new operator
Originally Posted by Moschops
Not by me
Well, okay, you should use container classes if they are more appropriate. Happy?
-
December 1st, 2010, 12:31 PM
#8
Re: C++ new operator
At risk of this ending up way off-topic, "always" is a very blanket statement.
As for smart pointers, I've always tended towards using them when I need them, but I have found that I very rarely need them; if I have trouble keeping track of objects on my heap, it's frequently due to bad design or inadequate planning, and by stepping back and reviewing the design I can usually simplify things to the point that I don't need them.
Obviously that's just my take on it, but it means that I don't recommend always using smart pointers; I recommend simplicity in design and more design and planning in advance.
I'm trying hard to keep this about code and not have a go at anyone's preferred methods of coding; everybody thinks differently to reach the same ends and code is in many ways a formalised expression of thought.
-
December 1st, 2010, 12:36 PM
#9
Re: C++ new operator
Originally Posted by Moschops
As for smart pointers, I've always tended towards using them when I need them, but I have found that I very rarely need them; if I have trouble keeping track of objects on my heap, it's frequently due to bad design or inadequate planning, and by stepping back and reviewing the design I can usually simplify things to the point that I don't need them.
Ah, but of course if you can simplify such that you don't have to keep track of those objects then that is great, and is precisely the point of using smart pointers and containers.
-
December 1st, 2010, 12:44 PM
#10
Re: C++ new operator
I appreciate what you're saying; to my mind, that's not a simplification, but instead hiding complexity in a black box.
I do often use container classes, but again, where appropriate. I come from a coding background (and often still work there) in which memory is precious - needing a single extra memory chip on something that will be manufactured a million times costs someone a lot of unnecessary expense, and as such keeping precise track of memory allocations becomes a habit.
I think what I'm saying here is horses for courses.
-
December 1st, 2010, 12:46 PM
#11
Re: C++ new operator
Originally Posted by Moschops
I appreciate what you're saying; to my mind, that's not a simplification, but instead hiding complexity in a black box.
Perhaps I misread you. What I am talking about is say, choosing to simplify from a std::vector<X*> to a std::vector<X> instead of reaching for a std::vector<std::tr1::shared_ptr<X> > or a boost::ptr_vector<X>.
-
December 1st, 2010, 12:57 PM
#12
Re: C++ new operator
Originally Posted by Moschops
I come from a coding background (and often still work there) in which memory is precious - needing a single extra memory chip on something that will be manufactured a million times costs someone a lot of unnecessary expense, and as such keeping precise track of memory allocations becomes a habit.
Oh, well for embedded work an entirely different set of priorities applies. In an unrestricted system code elegance should be the top priority, but as soon as you run into speed or memory limitations of course your priorities change.
-
December 1st, 2010, 01:14 PM
#13
Re: C++ new operator
Thanks for the tips everyone, however until I get a basic knowledge of c++ and feel comfortable with it, I want to stay with the basics. Afterward, I'll feel around into more simple or more practical ways of doing things
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|