Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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.

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the stages in the development cycle?

1058


Write down the equivalent pointer expression for referring the same element a[i][j][k][l]?

1255


What is runtime polymorphism in c++?

1151


What is abstract class in c++?

1041


What are the various situations where a copy constructor is invoked?

1126


Can manipulators fall in love?

1003


What is functions syntax in c++?

1130


How do you invoke a base member function from a derived class in which you have not overridden that function?

1118


What methods can be overridden in java?

1254


What do nonglobal variables default to a) auto b) register c) static

1145


What is the extraction operator and what does it do?

1081


What is c++ good for?

1057


How the programmer of a class should decide whether to declare member function or a friend function?

1115


Why is it necessary to use a reference in the argument to the copy constructor?

1113


How many types of scopes are there in c++?

1099