[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

when does the memory get officially released?

J.W.

8/30/2008 2:21:00 AM

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?


8 Answers

Ian Collins

8/30/2008 2:31:00 AM

0

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.

Rajib

8/30/2008 2:52:00 AM

0

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

LR

8/30/2008 3:12:00 AM

0

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

J.W.

8/30/2008 3:48:00 AM

0

Thanks for all the detailed responses.


peter koch

8/30/2008 9:39:00 AM

0

On 30 Aug., 04:30, Ian Collins <ian-n...@hotmail.com> 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

Kai-Uwe Bux

8/30/2008 10:02:00 AM

0

peter koch wrote:

> On 30 Aug., 04:30, Ian Collins <ian-n...@hotmail.com> 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

Ian Collins

8/30/2008 11:33:00 AM

0

peter koch wrote:
> On 30 Aug., 04:30, Ian Collins <ian-n...@hotmail.com> 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.

Old Wolf

9/1/2008 1:58:00 AM

0

On Aug 30, 2:21 pm, Jianwei Sun <jsunnewsgr...@gmail.com> 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