Thread: What happens to dynamic objects that are instantiated as parameters?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    What happens to dynamic objects that are instantiated as parameters?

    If there were code like
    Code:
    beginning_of_scope
    some_function(new some_object);
    end_of_scope
    What happens to the new instance of some_object after exiting end_of_scope? Is it freed automatically? Or is this a potential memory leak?

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    that leaks some_object.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It depends on what some_function() does.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    So if I don't have a corresponding "delete" statement in some_function, this is a memory leak?

    So I guess if you have a function that takes a pointer parameter, but has no idea where the referenced object was instantiated, then you should just do
    Code:
    some_object* so = new some_object;
    some_function(so);
    delete so;
    rather than doing what I did above, just to save about two lines of code. Oh, well.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you are going to do that, you should just write:
    Code:
    some_object so;
    some_function(&so);
    EDIT:
    The problem is, what does some_function() do? Suppose that it will destroy the object pointed to via the pointer. Your second example would result in a double deletion and my example would result in an attempt to use delete on a pointer that points to a stack allocated object. Your first example would then be correct.

    On the other hand, if some_function() does not destroy the object pointed to via the pointer, then your first example is wrong as it results in a memory leak.
    Last edited by laserlight; 02-08-2009 at 08:52 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you need to use dynamic memory, you should look into using smart pointers like the ones in the Boost library -- they automatically delete themselves when nothing is referencing them anymore.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And just as importantly, they document intent. If the function parameter is a std::auto_ptr, the function will take possession of the object and you're to leave it alone after the function call. If it takes a shared_ptr, it will want to hold on to the object, but so can you. If it takes a reference, it won't hold on to the object at all, and - if you use these consistently - if it takes a pointer, it doesn't either, and the argument is optional or an array.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointers to dynamic objects
    By mike_g in forum C++ Programming
    Replies: 20
    Last Post: 04-25-2007, 01:16 PM
  2. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  3. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 04:32 PM
  4. dynamic array of objects
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2003, 09:35 AM
  5. Dynamic list of Objects in External File
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2002, 02:05 PM