pointer and reference confuse

K

key9

Hi All

simple question:

1.====================================

{
int* p_1 = new int();
};
//will p1 leak?

2.====================================
class MyClass
{

public:
MyClass(){
p_2 = new int();
}


private:
int* p_2;
};

MyClass* mc_ = new MyClass();
delete mc_;
//will p_2 leak?


3.====================================

int* p_3 = new int();

{
int& r_1 = *p_3
};

//will p_3 hang?

4.====================================

I always confuse on these "will hang,will not hang...,will leak ,will not
leak"
, any knack?


thank you very much.


key9
 
J

Jakob Bieling

key9 said:
{
int* p_1 = new int();
};
//will p1 leak?

Yes, because there is no way to call 'delete' with the pointer value
that the above 'new' gave you.
class MyClass
{

public:
MyClass(){
p_2 = new int();
}
private:
int* p_2;
};
MyClass* mc_ = new MyClass();
delete mc_;
//will p_2 leak?

Yes, because again, you are not calling 'delete' for all pointers
that where returned by 'new'. Remember this rule:

a) Every call to 'new' needs a matching call to 'delete'and
b) every call to 'new[]' needs matching call to 'delete[]'

In the case above, you are missing the matching 'delete' for 'p_2'.
int* p_3 = new int();

{
int& r_1 = *p_3
};
//will p_3 hang?

Hang? The code will work, but again, you must "delete p_3;" after
you are done using it. As it is there, you have a memory leak.

hth
 
G

Gavin Deane

key9 said:
Hi All

simple question:

1.====================================

{
int* p_1 = new int();
};
//will p1 leak?

Yes. To avoid leaks, you must match every new with delete and every new
[] with delete []. p_1 is the only pointer pointing to the dynamically
allocated memory. When p_1 goes out of scope and is destroyed you can't
delete the memory because you have no pointer to pass to delete.
2.====================================
class MyClass
{

public:
MyClass(){
p_2 = new int();
}


private:
int* p_2;
};

MyClass* mc_ = new MyClass();
delete mc_;
//will p_2 leak?

Yes. There is no delete to match the new that was used to assign a
value to p_2. And when mc_ is destroyed, it's member p_2 is destroyed
and you have exactly the same problem as before. Some memory has been
allocated but you no longer have any pointers that point to it so you
can't deallocate the memory.

The destructor of MyClass should deallocate the memory. Bear in mind
that you will then need to write (or disable) the copy contructor and
assignment operator.
3.====================================

int* p_3 = new int();

{
int& r_1 = *p_3
};

//will p_3 hang?

p_3 is still in scope here so you can delete p_3; and you have no
problem. r_1 has already gone out of scope so is irrelevant. Were you
thinking of this?

int pi = new int;
int& ri = *pi;
delete pi;
// The int that ri refers to has been destroyed.
// ri is now a dangling reference.
4.====================================

I always confuse on these "will hang,will not hang...,will leak ,will not
leak"
, any knack?

Read the FAQ
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html

Once you've read that, make sure you always use standard library
classes and RAII techniques so you never have this sort of problem in
real code. new and delete should appear rarely in you code. When they
do appear, the memory should be managed by a class designed for the
purpose.

Gavin Deane
 
B

Ben Pope

key9 said:
Hi All

simple question:

1.====================================

{
int* p_1 = new int();
};
//will p1 leak?

Is there a matching delete for the new p_1?
2.====================================
class MyClass
{

public:
MyClass(){
p_2 = new int();
}


private:
int* p_2;
};

MyClass* mc_ = new MyClass();
delete mc_;
//will p_2 leak?

Again, is there a matching delete for the new p_2?
3.====================================

int* p_3 = new int();

{
int& r_1 = *p_3
};

//will p_3 hang?

hang is not defined by the standard.
4.====================================

I always confuse on these "will hang,will not hang...,will leak ,will not
leak"
, any knack?

Is this homework?

The knack is to know what happens when a scope *is* left. Do you
understand the construction and destruction process, and how scope
affects that?

You also need to know when a scope *can* be left, and it's not always at
the end of it.

Each call to new has to have 1 and only one matching call to delete.
Similarly for new[] and delete[].

There are techniques which are used to minimise the problems of
determining when to deallocate a resource (such as memory allocated with
new, by calling delete). This is called RAII (Resource Acquisition Is
Initilisation). It is leveraged by the use of resource allocation and
deallocation with the lifetime of an object. I.e., an object manages
the resource by obtaining the resource in the constructor, and releasing
the resource in the destructor.

Aggressive use of RAII techniques makes for robust code.

Ben Pope
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top