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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Difference between strdup and strcpy?

1146


What will happen if a pointer is deleted twice?

1137


Array base access faster or pointer base access is faster?

2251


explain the reference variable in c++?

1020


Of the numbers 12 23 9 28 which would be at the top of a properly implemented maxheap a) 28 b) 9 c) Any of them could be

1367


Can we define function inside main in c++?

960


What is the benefit of learning c++?

938


What is vector string in c++?

1046


what are the types of Member Functions?

1089


Show the application of a dynamic array with the help of an example.

1085


Is it possible to get the source code back from binary file?

1298


What are the various access specifiers in c++?

988


What is null and void pointer?

1008


Will a catch statement catch a derived exception if it is looking for the base class?

966


How much do c++ programmers make?

1067