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

What are the advantages of C++ programming compared to C programming?

2 Answers   HAL,


Declare a class vehicle and make it an abstract data type.

0 Answers  


Is turbo c++ free?

0 Answers  


What is c++ similar to?

0 Answers  


Can class objects be passed as function arguments?

0 Answers   HCL,






What it is and how it might be called (2 methods).

0 Answers  


Is java based off c++?

0 Answers  


write a program that withdrawals,deposits,balance check,shows mini statement. (using functions,pointers and arrays)

0 Answers  


What is the use of turbo c++?

0 Answers  


If dog is a friend of boy, and terrier derives from dog, is terrier a friend of boy?

0 Answers  


Explain differences between alloc() and free()?

0 Answers  


Difference between class and structure.

0 Answers  


Categories