In a class, there is a reference or pointer of an object of
another class embedded, and the memory is either allocated
or assigned to the new object created for this class. In
the constructor, parameters are passed to initialize the
data members and the embedded object reference to get
inialized. What measures or design change should be advised
for proper destruction and avioding memory leaks, getting
pointers dangling for the embedded object memory
allocation? Please suggest.

Answers were Sorted based on User's Feedback



In a class, there is a reference or pointer of an object of another class embedded, and the memory..

Answer / ravindranath m

Embedded object is of reference type. That is, its possible
that the constructor of the container class would receive an
argument that is a reference to the embedded class object.

So, there should be a copy-constructor and/or
copy-assignment operator implemented for the embedded
object's class.

As stated in the problem statement, the embedded object ptr
is "either allocated memory" or "assigned memory".

Since the embedded object is a *reference* to an object, its
quite possible that actual object of embedded type is
constructed elsewhere and passed as an arg to the container
class. This also means that someone outside the container
class is also having a reference to the embedded class. It
implies that this embedded object can be deleted outside the
class, in which case the embedded object ptr may become a
dangling ptr.

The best approach seems to be the implementation of smart
pointer aka shared pointer, which can do reference counting
and destroy itself only when no more references are present.

see www.boost.org (or google search) for information on
shared pointers.

Is This Answer Correct ?    2 Yes 0 No

In a class, there is a reference or pointer of an object of another class embedded, and the memory..

Answer / mms zubeir

The most common design is to delete the object reference in
the destructor of the contained class. This will not be
helpful in cases where an exception is thrown.

There are two objects here viz, the container object and
the embedded object.

If the embedded object construction fails, no problem, it
will not cause any memory leaks since there will be no
memory allocated for it.

If the container object construction fails after
constructing the embedded object, there is a chance of
memory leak. We can use exception handling mechanism to
deallocate the object's space.

1. If the embedded object reference is not inside the try
block, then we can use that reference in the catch block to
delete in case of exceptions.

2. Suppose the embedded object reference/pointer is local
to the try block. This time, the reference to it won't be
available in catch block too. So we will not get a chance
to destroy it even in catch block also. So we need to keep
such kind of references in a global variable and will be
available anywhere in the application. So we can delete the
object reference safely.

Is This Answer Correct ?    1 Yes 0 No

In a class, there is a reference or pointer of an object of another class embedded, and the memory..

Answer / shanthila

use copy constructors

Is This Answer Correct ?    1 Yes 1 No

In a class, there is a reference or pointer of an object of another class embedded, and the memory..

Answer / mms zubeir

Sorry, there is an error in my above answer. In the first
statement, it is container class, not contained class.

Is This Answer Correct ?    0 Yes 0 No

In a class, there is a reference or pointer of an object of another class embedded, and the memory..

Answer / som shekhar

If you are using the reference orpointer of other class as
an Aggregation (class own every thing, tight coupling) then
the destructor should take care of it, but if the reference
or pointer is taken as composition (class doesnt owwn the
pointer, loose coupling) then the class destructor should
not worry about that.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

Why preincrement operator is faster than postincrement?

5 Answers  


Const char *p , char const *p What is the difference between the above two?

0 Answers   TCS,


What do you mean by volatile and mutable keywords used in c++?

0 Answers  


Explain the operation of overloading of an assignment operator.

0 Answers  


How Virtual functions call up is maintained?

2 Answers  






Given a simple program designed to take inputs of integers from 1-1000 and to output the factorial value of that number, how would you test this program? You do not have access to the code. Please be as specific as possible.

4 Answers   Microsoft,


How do I make turbo c++ full screen?

0 Answers  


Write about the scope resolution operator?

0 Answers  


How a new operator differs from the operator new?

0 Answers  


Explain the difference between c & c++?

0 Answers  


Why c++ is so important?

0 Answers  


What is this pointer in c++?

1 Answers  


Categories