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
Reasoning for the observed behavior:
The compiler uses the conversion constructor in derived class,
for evaluating:
It takes in the integer
40and creates abarobject using the conversion constructor and then assigns the createdbarobject toausing the implicit copy assignment operator(=) generated by the compiler for classbar.This is the reason you see additional calls to
barconstructor and that Base class overloaded=is never called. Also, the reason for double free lies in here, You have multiplebarobjects which keep pointing to the dynamic memory allocated forptr_xand 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.