views:

457

answers:

11

Lets say there is a base class pointer which is pointing to a base class object:

baseclass *bptr;
bptr= new baseclass;

now if i do

bptr= new derived;

what is the problem here?

+13  A: 

The obvious answer is that you're leaking the original base class object.

Tetrad
this was my answer ...actually ..but he hinted me of any other problem that i could figure out.
Vijay Sarathi
There is no other problem.
Blindy
One thing could be that the derived must actually be derived from base.
ckv
@benjamin button: If the base class destructor is not virtual then when you `delete bptr;` the derived class destructor is not called. But I don't know whether it is relevant here.
Naveen
@Naveen: that is true, but if that's the answer the interviewer was looking for, then I'd say it was rather a trick question... it seems reasonable to me to assume that if a class is designed to be inherited from then it would have a virtual destructor.
Dean Harding
@codeka: I agree..but the virtual destructor is a rather common interview question, so just a guess.
Naveen
@Naveen: That's plain wrong. If the base class doesn't have a virtual destructor that's undefined behavior, not some destructor not being called.
sharptooth
We could add that, if either `baseclass` or `derived` have no parameterless constructor, the code will not compile. Silly, indeed, but no sillier than having you guess whether the classes are correctly written or not when you cannot see them. Besides, if the answer is related to the lack of a virtual destructor, the second line is unnecessary.
Gorpik
why does it leak?
iterationx
A: 

You are reassigning the pointer without releasing the memory allocated for the previous object. This can result in memory leak, in case no body else has stored the value of bptr before reassigning it.

Naveen
+1  A: 

In first line, you declared a pointer. In second line, you give it a reference in memory. in line 3, you give IT another reference in memory.

the first reference you gave to the pointer is leaked...you can never get it back.

VoodooChild
A: 

Does baseclass have a virtual destructor? If not, that could be bad too (in addition to obvious leak).

Brian
+7  A: 

Other than the obvious memory leak it depends on the definition of baseclass and derived some of the problems could be:

  1. derived is not publicly derived from baseclass (if so it's a horrible question to ask in an interview)
  2. baseclass does not have a virtual destructor (in which case it's unsafe to delete bptr;)
Motti
+1  A: 

Was he trying to get to the Circle Ellipse problem? If derived is a specialisation of baseclass some public methods on baseclass may no longer have a meaning e.g. if Circle is derived from Ellipse and Ellipse has a method called stretch() which stretches the ellipse in one axis, what do you do when the method is called on an instance of Circle?

JeremyP
A: 

The only problem occurs if the class destructor is not defined as virtual. In this scenario, it will cause memory leak

Pardeep
A: 

There is no syntax problem but you are loosing the address of the "baseclass" instance by assigning a new value to "bptr" variable. This results in a memory leak, as the address of the first instanciated class is lost.

Iulian Şerbănoiu
+4  A: 

If I were you I would turn back and ask your interviewer couple of questions:

  1. Are you sure this is the only code snippet available and in that case my answer's an educated guess, nothing more.
  2. Is there some sort of a memory mgmt scheme for baseclass and derived? In that case we need to look into the operator new definitions which ain't provided.
  3. If no memory mgmt, then are baseclass or derived smart pointer types that do reference counting? No leaks in those cases either.
  4. If no smart types, then of course this looks like a memory leak.

And please, having virtual destructors etc come into place only if you have the class definitions. Perhaps the interviewer is secretly looking for clairvoyant types.

Arpan

Fanatic23
+1, Sometimes questions can also be the answers.. :)
liaK
+2  A: 

what is the problem here?

If the answer of ("a memory leak") is not accepted, the problem is either the question or the interviewer ;)

utnapistim
A: 

I'd answer this interview question like so:

From the limited code snippet provided it's clear that a memory leak will ensue when we assign the pointer to an instance of this derived class, as we have no other pointers accessing the base object. However it's impossible to say anymore about the code snippet without seeing the definitions of both classes, and seeing the relationship between the two classes.

toby