Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > when does the memory get officially released?

Reply
Thread Tools

when does the memory get officially released?

 
 
Jianwei Sun
Guest
Posts: n/a
 
      08-30-2008
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?


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-30-2008
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.
 
Reply With Quote
 
 
 
 
Rajib
Guest
Posts: n/a
 
      08-30-2008
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
 
Reply With Quote
 
LR
Guest
Posts: n/a
 
      08-30-2008
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

 
Reply With Quote
 
Jianwei Sun
Guest
Posts: n/a
 
      08-30-2008
Thanks for all the detailed responses.


 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      08-30-2008
On 30 Aug., 04:30, Ian Collins <(E-Mail Removed)> 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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-30-2008
peter koch wrote:

> On 30 Aug., 04:30, Ian Collins <(E-Mail Removed)> 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

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-30-2008
peter koch wrote:
> On 30 Aug., 04:30, Ian Collins <(E-Mail Removed)> 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.
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      09-01-2008
On Aug 30, 2:21*pm, Jianwei Sun <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beyond The Mind's Eye DVD - Ever Officially Released? TB DVD Video 2 11-03-2005 07:26 PM
WebMatrix officially dead? Patrick Olurotimi Ige ASP .Net 5 04-29-2005 03:27 PM
Blockbuster officially drops VHS? RichA DVD Video 5 12-16-2004 01:03 PM
OT: The Officially MCNGP Sponsored =?iso-8859-1?Q?Frisbee=AE?= MCSE 10 07-30-2004 05:32 PM
S.W.A.T DVD Officially Announced, Specs, Release Date: Dec 30th Mike McGee DVD Video 1 10-20-2003 08:57 PM



Advertisments