1

I have the following sample code. class bar is derived from the base class foo and allocates memory for ptr_x while read/write access are granted through the base class. This is a toy model for a large code in which read/write functions are the same for all different variants of an object but memory allocation slightly differs in different variants.

#include <iostream>

class foo{
protected:
    int *ptr_x;

public:
    foo(){
        std::cout << " 1) Inside foo constructor: " << ptr_x << std::endl;
    }

    void read() 
    {
        std::cout << " 2) Inside read function: " <<  ptr_x << std::endl;    
        std::cout << " 3) Inside read function: " << *ptr_x << std::endl;
    }
    void operator=(const int rhs)
    {
        std::cout << " 4) Inside operator= : " <<  ptr_x << std::endl;
        *ptr_x = rhs;
    }        
};
class bar: public foo{

public:
    bar(int a)
    : foo()
    {
        std::cout << " 5) Inside bar constructor: " << ptr_x << std::endl;
        ptr_x = new int;
        std::cout << " 6) Inside bar constructor: " << ptr_x << std::endl;
        *ptr_x = a;
    }
    ~bar()
    {
        std::cout << " 7) Inside bar destructor: " << ptr_x << std::endl;
        if (ptr_x != NULL) {delete ptr_x; ptr_x = NULL;}
    }
};
int main (){
    bar a(20);
    a.read();
    a = 40;
    a.read();
    return 0;
}

When I run the code, I get:

 1) Inside foo constructor: 0
 5) Inside bar constructor: 0
 6) Inside bar constructor: 0x1d8f010
 2) Inside read function: 0x1d8f010
 3) Inside read function: 20
 1) Inside foo constructor: 0x7f40c11e3b68
 5) Inside bar constructor: 0x7f40c11e3b68
 6) Inside bar constructor: 0x1d8f030
 7) Inside bar destructor: 0x1d8f030
 2) Inside read function: 0x1d8f030
 3) Inside read function: 0
 7) Inside bar destructor: 0x1d8f030
*** glibc detected *** ./a.out: double free or corruption (fasttop): 0x0000000001d8f030 ***

I have the following questions: 1) Why does not code enter operator= in the base class? 2) Why is there a second call made to the constructor/destructor ? 3) why is there a double free problem?

what am I doing wrong here?

EDIT: Ok double free problem is obvious, but why is there two instances of same memory locations? This somehow has to do with operator= since when I comment it out everything is fine.

Thanks

mmirzadeh
  • 6,893
  • 8
  • 36
  • 47

2 Answers2

2

Reasoning for the observed behavior:

The compiler uses the conversion constructor in derived class,

  bar(int a)

for evaluating:

  a = 40;

It takes in the integer 40 and creates a bar object using the conversion constructor and then assigns the created bar object to a using the implicit copy assignment operator(=) generated by the compiler for class bar.

This is the reason you see additional calls to bar constructor and that Base class overloaded = is never called. Also, the reason for double free lies in here, You have multiple bar objects which keep pointing to the dynamic memory allocated for ptr_x and when one of the object goes out of scope the destructor is called which deallocates the memory but leaves the other objects member pointer in a dangling state.

How to solve these problems:
You should mark the conversion constructor explicit if you don't want the compiler to use it for implicit conversions like this.

You should follow the Rule of Three for your class bar.

Caveat:
Note that the = operator in the base classes is always hidden by the implicitly generated = operator for the derived class.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Thanks. Does this mean I always have to define the `=` operator for derived classes? What are operators are like this? – mmirzadeh Feb 17 '12 at 06:44
  • @GradGuy: If you don't redefine the `=` operator for your derived class the compiler implicitly generates it thus hiding the base class `=`. Read more about [Function Hiding](http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9), that should give you a good idea of where the same situation will apply. – Alok Save Feb 17 '12 at 07:04
1

For the first question you should have declared bar (int a) as

explicit bar(int a).

this gives a proper compilation error that class bar should have = operator defined.

Vijay
  • 65,327
  • 90
  • 227
  • 319