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
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 |
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 |
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 |
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 |
What is microsoft c++ redistributable?
What happens if an exception is throws from an, object's constructor and object's destructor?
What is runtime errors c++?
If P is the population on the first day of the year, B is the birth rate, and D is the death rate, the estimated population at the end of the year is given by the formula: The population growth rate is given by the formula: B – D Write a program that prompts the user to enter the starting population, birth and death rates, and n, the number of years. The program should then calculate and print the estimated population after n years. Your program must have at least the following functions: 1. growthRate: This function takes its parameters the birth and death rates, and it returns the population growth rate. 2. estimatedPopulation: This function takes its parameters the current population, population growth rate, and n, the number of years. It returns the estimated population after n years Your program should not accept a negative birth rate, negative death rate, or a population less than 2.
Why is c++ still used?
Should I learn c++ c?
What is an accessor in c++?
Define precondition and post-condition to a member function?
Where is atoi defined?
program explaining feautures of c++
What is difference between c++ and c ++ 14?
Write about the various sections of the executable image?