What happens to dynamic objects that are instantiated as parameters?

This is a discussion on What happens to dynamic objects that are instantiated as parameters? within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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
    839
    that leaks some_object.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    It depends on what some_function() does.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,256
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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, 02: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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21