I am getting an invalid write of size 8 with valgrind for the following code. Any help in this regard please.
#include <string>
#include <iostream>
using namespace std;
class Base {
public:
Base() {
cout << "Base constructor ... ";
m_strPath = "";
m_total = 0;
cout << "Completed Base constructor" << endl;
}
~Base() {
cout << "Base destructor ... ";
m_strPath = "";
m_total = 0;
cout << "Completed Base destructor" << endl;
}
protected:
string m_strPath;
unsigned int m_total;
};
class Derived : public Base {
public:
Derived() {
m_offset = 0;
cout << " Size of offset (off_t) = "
<< sizeof(m_offset) << endl;
}
~Derived() {
cout << "Derived destructor" << endl;
}
private:
off_t m_offset;
};
int main() {
cout << "Starting execution" << endl;
Derived *listFile = new Derived();
cout << " size of dynamic object is = "
<< sizeof(*listFile) << endl;
delete listFile;
cout << "-----" << endl;
Derived listFile1;
cout << " size of object is = " << sizeof(listFile1) << endl;
cout << "Ended execution" << endl;
return 0;
}
The valgrind error points me to offset member variable of derived class. "Invalid write of size 8" happens for both object created on stack and on heap.
Thanks