when does the memory get officially released?

J

Jianwei Sun

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?
 
I

Ian Collins

Jianwei said:
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.
 
R

Rajib

Jianwei said:
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
 
L

LR

Jianwei said:
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
 
K

Kai-Uwe Bux

peter said:
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
 
I

Ian Collins

peter said:
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.
 
O

Old Wolf

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top