Seg Fault using overloaded new operator.
Jun 12, 2009 at 4:21am UTC
Could someone please explain why i get a seg fault at the place indicated below.
Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include <iostream>
#include <memory>
#include <exception>
#include <new>
using namespace std;
class Base
{
int data;
public :
Base(int arg_data = 0) : data(arg_data)
{
cout<<"Base::Base()" <<endl;
}
void * operator new [] (size_t sz) throw (bad_alloc);
void operator delete [] (void *);
virtual void foo()
{
cout<<"Base::foo()" <<endl;
};
virtual ~Base()
{
cout<<"Base::~Base()" <<endl;
}
};
class Derv : public Base
{
int data;
public :
Derv(int p_data = 0) : Base(p_data), data(p_data)
{
cout<<"Derv::Derv()" <<endl;
}
void foo()
{
cout<<"Derv::foo()" <<endl;
}
~Derv()
{
cout<<"Derv::~Derv()" <<endl;
}
};
void * Base::operator new [] (size_t sz) throw (bad_alloc)
{
cout<<"Base::operator new[](), size = " <<sz<<endl;
return ::new char [sz];
}
void Base::operator delete [](void * poVoid)
{
cout<<"Base::operator delete[]()" <<endl;
::delete []poVoid;
}
int main()
{
Base* poBase = new Derv[20];
poBase->foo();
delete []poBase; // I get a seg fault here?? dont know why!!
return 0;
}
Output=
Base::operator new[](), size = 244
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Base::Base()
Derv::Derv()
Derv::foo()
12 [main] a 596 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
Segmentation fault (core dumped)
Jun 12, 2009 at 8:28pm UTC
This might well be a compiler issue.
Couldn't get it to work with MINGW but works as expected in MSVC
Jun 13, 2009 at 12:54pm UTC
test.cpp:62: warning: deleting `void*' is undefined
Why is that function taking a void * instead of a Base *?
Jun 14, 2009 at 7:03am UTC
Helios
There are 2 reasons for this:
1] delete takes a void* as a first parameter; and
2] delete gets called only after the d'tors have executed. The d'tors destroy the objects; so what remains is only raw memory; so it takes a void* & not a Base*..
Cheerz
Rajat
guestgulkan
i did not try that code with any other compiler. Thanks for letting me know.
Jun 14, 2009 at 7:22am UTC
Actually, the code is wrong.
Base* poBase = new Derv[20];
This is a wierd puzzle. When you call new, you allocate sizeof(Derv) * 20. Apparently Base has a different size and when you delete it, it causes the seg fault.
That's why this works:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <iostream>
using namespace std;
class Base
{
int data;
public :
Base(int arg_data = 0) : data(arg_data)
{
cout<<"Base::Base()" <<endl;
}
void * operator new [] (size_t sz) throw (bad_alloc);
void operator delete [] (void *);
virtual void foo()
{
cout<<"Base::foo()" <<endl;
};
virtual ~Base()
{
cout<<"Base::~Base()" <<endl;
}
};
class Derv : public Base
{
};
void * Base::operator new [] (size_t sz) throw (bad_alloc)
{
cout<<"Base::operator new[](), size = " <<sz<<endl;
return ::new char [sz];
}
void Base::operator delete [](void * poVoid)
{
cout<<"Base::operator delete[]()" <<endl;
::delete []poVoid;
}
int main()
{
Base* poBase = new Derv[20];
poBase->foo();
delete []poBase;
return 0;
}
In this case sizeof(Derv) = sizeof(Base);
The reason the reason it allows you to set a Base* to a Derv* is most likely because Derv* is derived from Base* and in a sense, it's a hack or bug.
Last edited on Jun 14, 2009 at 7:38am UTC
Topic archived. No new replies allowed.