when does the memory get officially released?

Discussion in 'C++' started by Jianwei Sun, Aug 30, 2008.

  1. Jianwei Sun

    Jianwei Sun Guest

    I have a question, it may sound stupid. In c#/Java , when you create an object
    on the heap and don't reference anymore, the memory will be cleared out when
    garbage collection walk thourough the memory region.

    But in C++, if I do:


    Object* pObj=new Object();
    delete pObj;

    Then will the memory be cleared right away? Or when we do delete pObj, it
    actually *just* break the reference to the object in the heap, so it will
    be ready to be cleared. Thanks.

    If I do this:

    Object* pObj=new Object();
    Object** ppObj=new Object*;
    *ppObj=pObj;
    delete ppObj;

    The object will still be availble after delete, right?
    Jianwei Sun, Aug 30, 2008
    #1
    1. Advertising

  2. Jianwei Sun

    Ian Collins Guest

    Jianwei Sun wrote:
    > I have a question, it may sound stupid. In c#/Java , when you create an
    > object on the heap and don't reference anymore, the memory will be
    > cleared out when garbage collection walk thourough the memory region.
    >
    > But in C++, if I do:
    >
    >
    > Object* pObj=new Object();
    > delete pObj;
    >
    > Then will the memory be cleared right away? Or when we do delete pObj,
    > it actually *just* break the reference to the object in the heap, so it
    > will be ready to be cleared. Thanks.
    >

    It might be and as far as your code is concerned it has. After delete,
    pObj will be invalid and Object's destructor will have been called. The
    memory isn't cleared, but it is available for future allocations.

    > If I do this:
    >
    > Object* pObj=new Object();
    > Object** ppObj=new Object*;
    > *ppObj=pObj;
    > delete ppObj;
    >
    > The object will still be availble after delete, right?
    >


    Yes.

    --
    Ian Collins.
    Ian Collins, Aug 30, 2008
    #2
    1. Advertising

  3. Jianwei Sun

    Rajib Guest

    Jianwei Sun wrote:
    > I have a question, it may sound stupid. In c#/Java , when you create an
    > object on the heap and don't reference anymore, the memory will be
    > cleared out when garbage collection walk thourough the memory region.
    >
    > But in C++, if I do:
    >
    >
    > Object* pObj=new Object();
    > delete pObj;
    >
    > Then will the memory be cleared right away? Or when we do delete pObj,
    > it actually *just* break the reference to the object in the heap, so it
    > will be ready to be cleared. Thanks.
    >
    > If I do this:
    >
    > Object* pObj=new Object();
    > Object** ppObj=new Object*;
    > *ppObj=pObj;
    > delete ppObj;
    >
    > The object will still be availble after delete, right?
    >
    >


    Keep in mind that delete only frees the memory, it doesn't set pObj to
    NULL (is this what you mean by "break the reference"?). After you call
    delete pObj will point to an area of memory that is already freed, so
    you'll get undefined behavior when you try to use it. This is why one
    often sees code like:

    delete pObj;
    pObj=0; //or NULL
    Rajib, Aug 30, 2008
    #3
  4. Jianwei Sun

    LR Guest

    Jianwei Sun wrote:
    > I have a question, it may sound stupid. In c#/Java , when you create an object
    > on the heap and don't reference anymore, the memory will be cleared out when
    > garbage collection walk thourough the memory region.


    What do you mean by "cleared out"?

    >
    > But in C++, if I do:
    >
    >
    > Object* pObj=new Object();


    Loosely speaking, the above allocates memory for an instance of Object,
    and then constructs the instance in that memory. pObj is set to the
    address of the memory that was allocated.

    > delete pObj;


    This will call ~Object() for the object located at the address stored in
    pObj. Then, it's likely that the memory will be returned to some
    available pool at this point, where it might be returned by another new.



    > Then will the memory be cleared right away? Or when we do delete pObj, it
    > actually *just* break the reference to the object in the heap, so it will
    > be ready to be cleared. Thanks.


    I suspect this is more correct, but I'm still not sure what you mean by
    cleared. I think it's possible that memory isn't "cleared" depending on
    what you mean.





    > If I do this:
    >
    > Object* pObj=new Object();


    Creates an instance of Object and sets pObj to point to this instance.


    > Object** ppObj=new Object*;


    Create an uninitialized pointer to Object and set ppObj to point to this
    pointer.


    > *ppObj=pObj;


    Now ppObj points to a pointer to the object pointed to by pObj.


    > delete ppObj;


    Delete the pointer to the pointer to Object that we just initialized.
    But this doesn't delete the instance of Object that pObj still points to.



    > The object will still be availble after delete, right?


    It will be, because you haven't deleted the instance of Object that pObj
    still points to.

    Your snippet, as it stands has a memory leak.


    If you change the last line to:
    delete *ppObj;
    the instance of Object pointed to by pObj will be destroyed.


    Might I suggest that you try this with this class:

    class Object {
    public:
    Object() {
    std::cout << "Object()" << std::endl;
    }
    ~Object() {
    std::cout << "~Object()" << std::endl;
    }
    };

    Also, consider that if I run this little program,
    struct Object {};

    int main() {
    Object *a = new Object();
    Object *p1 = a;
    delete a;

    Object *b = new Object;
    Object *p2 = b;
    delete b;

    const bool t = (p1 == p2);
    }

    The value of t is true before the program finishes. Of course, that
    might change at the next run, but it is possible that during some runs t
    will be true.

    LR
    LR, Aug 30, 2008
    #4
  5. Jianwei Sun

    Jianwei Sun Guest

    Thanks for all the detailed responses.
    Jianwei Sun, Aug 30, 2008
    #5
  6. Jianwei Sun

    peter koch Guest

    On 30 Aug., 04:30, Ian Collins <> wrote:
    > Jianwei Sun wrote:

    [snip]
    >
    > > If I do this:

    >
    > > Object* pObj=new Object();
    > > Object** ppObj=new Object*;
    > > *ppObj=pObj;
    > > delete ppObj;

    >
    > > The object will still be availble after delete, right?

    >
    > Yes.


    What object? The object pointed to by pObj will not - it is no more.
    The parrot is dead, to quote Monty Python - it is an ex-parrot.

    /Peter
    peter koch, Aug 30, 2008
    #6
  7. Jianwei Sun

    Kai-Uwe Bux Guest

    peter koch wrote:

    > On 30 Aug., 04:30, Ian Collins <> wrote:
    >> Jianwei Sun wrote:

    > [snip]
    >>
    >> > If I do this:

    >>
    >> > Object* pObj=new Object();
    >> > Object** ppObj=new Object*;
    >> > *ppObj=pObj;
    >> > delete ppObj;

    >>
    >> > The object will still be availble after delete, right?

    >>
    >> Yes.

    >
    > What object? The object pointed to by pObj will not - it is no more.
    > The parrot is dead, to quote Monty Python - it is an ex-parrot.


    Which of the four lines destroys the object pointed to by pObj? I only see a

    delete ppObj;

    not a

    delete *ppObj;

    nor a

    delete pObj;


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Aug 30, 2008
    #7
  8. Jianwei Sun

    Ian Collins Guest

    peter koch wrote:
    > On 30 Aug., 04:30, Ian Collins <> wrote:
    >> Jianwei Sun wrote:

    > [snip]
    >>> If I do this:
    >>> Object* pObj=new Object();
    >>> Object** ppObj=new Object*;
    >>> *ppObj=pObj;
    >>> delete ppObj;
    >>> The object will still be availble after delete, right?

    >> Yes.

    >
    > What object? The object pointed to by pObj will not - it is no more.
    > The parrot is dead, to quote Monty Python - it is an ex-parrot.
    >

    Nope, it's just resting. Tired and shagged out after a long squawk.

    --
    Ian Collins.
    Ian Collins, Aug 30, 2008
    #8
  9. Jianwei Sun

    Old Wolf Guest

    On Aug 30, 2:21 pm, Jianwei Sun <> wrote:
    > I have a question, it may sound stupid. In c#/Java , when you create an object
    > on the heap and don't reference anymore, the memory will be cleared out when
    > garbage collection walk thourough the memory region.
    >
    > If I do this:
    >
    > Object* pObj=new Object();
    > Object** ppObj=new Object*;


    Note that in C++ you only "new" things when
    you WANT to explicitly control when they get
    deleted. C++ has automatic object lifetime
    management, e.g.:
    Object obj;
    Object *pObj = &obj;
    //......
    // no manual deletion required
    Old Wolf, Sep 1, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Olurotimi Ige

    WebMatrix officially dead?

    Patrick Olurotimi Ige, Apr 29, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    522
    Scott Allen
    Apr 29, 2005
  2. Ralph_o_pedia_911

    E-Mail Marketing is now officially SPAM

    Ralph_o_pedia_911, Sep 22, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    471
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 22, 2006
  3. Gianni Galore
    Replies:
    1
    Views:
    829
    code learner
    Jan 7, 2011
  4. Fernando Perez

    [ANN] IPython 0.11 is officially out

    Fernando Perez, Jul 31, 2011, in forum: Python
    Replies:
    2
    Views:
    537
    Robert Kern
    Aug 2, 2011
  5. Urabe Shyouhei
    Replies:
    0
    Views:
    76
    Urabe Shyouhei
    May 31, 2008
Loading...

Share This Page