0

In my code, I have used three classes. See the implementation below:

class Medicine 
{ 
   int a;
}

class Pain:public Medicine 
{
   int b;
}

class Comb:public Pain   
{
    string salt,com;
}

All classes have just parameterized constructors. And call() is like

call()
{
     cout<<"You are in class the_name_of_the_class"<<endl;
}

I have defined a function with same name, call(), in all of them. (They are not declared as virtual till now)

The code goes like:

int main()
{       
    Pain *p[2];
    p[0]= new Comb("Salt","Com",2,110);
    p[1]= new Comb("SALT","COM",1,100);

    p[0]->call();

    delete p[0];
    delete p[1];
    return 0;
}

Output: Call goes to Pain’s call()

However, if I make Pain::call() as virtual (Medicine::call() is real), then call goes to Comb’s call(). No issue!

But when I am doing Medicine *p[2] instead of Pain *p[2], Following error is occuring

*** glibc detected *** ./a.out: free(): invalid pointer: 0x00000000022ed078 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3b64a760e6]
./a.out[0x400efe]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x3b64a1ecdd]
./a.out[0x400b79]
======= Memory map: ========

more things goes here, and this ending with

Abort(core dumped)

Why so? This again disappear when I use virtual for Medicine::call().(This problem is independent of whether Pain::call() is virtual or not). Why is this happening?

10
  • 2
    can you call real Medicine/Pain/Comb classes?
    – billz
    Aug 21, 2013 at 11:42
  • 1
    Can you post some minimal code that reproduces the problem? Aug 21, 2013 at 11:43
  • I am not able to get you. Please elaborate.
    – Smith
    Aug 21, 2013 at 11:44
  • Please provide a minimal implementation of the classes: The base class has a virtual dtor? is call() virtual? etc etc
    – Manu343726
    Aug 21, 2013 at 11:44
  • 1
    Please provide an SSCCE to avoid futile guesses about your class definitions.
    – Arne Mertz
    Aug 21, 2013 at 11:49

3 Answers 3

8

You're running into undefined behavior because the base class' destructor isn't virtual. Anything can happen.

If you delete a derived object through a pointer to the base class, the destructor must be virtual. It's a rule.

0

According to Scott Mayers"Effective C++" you should define destructor as virtual in base class if you are going to define derived class in the program otherwise the program will behave differently(possibly memory leak, wrong output, crash) during runtime.

0

Virtual destructors are useful when we can delete an instance of a derived class through a pointer to base class:

class Base 
{
   // some code
};

class Derived : public Base
{
    ~Derived()
    {
        // some code
    }
}

Here, I didn't declare Base's destructor to be virtual. Now, let's have a look at the following code:

Base *b = new Derived();
// use b
delete b; // Here's the problem!

Since Base's destructor is not virtual and b is a Base* pointing to a Derived object, delete b has undefined behaviour. In my implementation also, the call to the destructor will be resolved like any non-virtual code, meaning that the destructor of the base class will be called but not the one of the derived class, resulting in resources leak. Also, its always safe to declare destructor as virtual when you are using inheritence, whether it seems to be useful or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.