CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Dec 2010
    Posts
    12

    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    118

    Re: C++ new operator

    As written, line 2 will not work. The operator new returns a pointer.

    Code:
    new Echo;
    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

    Code:
    delete objName;
    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

    Code:
    Echo objName;
    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.

  3. #3
    Join Date
    Dec 2010
    Posts
    12

    Re: C++ new operator

    Excellent, very well explained thank you!

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: C++ new operator

    Quote Originally Posted by Moschops View Post
    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.

  5. #5
    Join Date
    Apr 2008
    Posts
    118

    Re: C++ new operator

    Quote Originally Posted by Lindley View Post
    It is recommended to always use smart pointer classes for storing heap-allocated objects.
    Not by me

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: C++ new operator

    Any particular reason, or are you just masochistic?

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C++ new operator

    Quote Originally Posted by Moschops
    Not by me
    Well, okay, you should use container classes if they are more appropriate. Happy?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Apr 2008
    Posts
    118

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C++ new operator

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Apr 2008
    Posts
    118

    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.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C++ new operator

    Quote 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>.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: C++ new operator

    Quote Originally Posted by Moschops View Post
    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.

  13. #13
    Join Date
    Dec 2010
    Posts
    12

    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
  •  





Click Here to Expand Forum to Full Width

Featured