473,412 Members | 2,075 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

curious behaviour in virtual destructor inside base class

Following test code behaviour suprised me and I dont know what's wrong
with this. I have two overloaded constructors within base class and
virtual destructor implemented. Derived class uses protected
non-parametrized constructor of base class which DOES NOT initialize
private member 'value'. But by calling both destructors virtually,
binary does not complaint about deleting uninitialized pointer. WHY ??
C++ compiler used: G++ 3.4.3 #include <iostream> using namespace std;
class Base { public: Base(int) : value( new int(-14) ) {}
virtual ~Base() { //cout << *value << endl; // throws
SEGFAULT as supposed delete value; // no SEGFAULT
here ?? WHY ?? } protected: /* Constructor overload for
derived classes */ Base() { // does not
initialize 'value' pointer !!! } private: int *value;
}; class Derived : public Base { public: Derived() : Base() {}
virtual ~Derived() {} }; int main() { Base *p =
new Derived; delete p; }

Oct 27 '05 #1
8 1744
TIT
LAvoisieR sade:
Following test code behaviour suprised me and I dont know what's wrong
with this. I have two overloaded constructors within base class and
virtual destructor implemented. Derived class uses protected
non-parametrized constructor of base class which DOES NOT initialize
private member 'value'. But by calling both destructors virtually,
binary does not complaint about deleting uninitialized pointer. WHY ??
C++ compiler used: G++ 3.4.3 #include <iostream> using namespace std;
class Base { public: Base(int) : value( new int(-14) ) {}
virtual ~Base() { //cout << *value << endl; // throws
SEGFAULT as supposed delete value; // no SEGFAULT
here ?? WHY ?? } protected: /* Constructor overload for
derived classes */ Base() { // does not
initialize 'value' pointer !!! } private: int *value;
}; class Derived : public Base { public: Derived() : Base() {}
virtual ~Derived() {} }; int main() { Base *p =
new Derived; delete p; }


My Eyes! AAhh!

TIT
Oct 27 '05 #2

LAvoisieR wrote:
Following test code behaviour suprised me and I dont know what's wrong .... delete value; // no SEGFAULT here ?? WHY ??


Because it's ok to delete a NULL pointer.
It is not ok to dereference it.

Cheers,
Andre

Oct 27 '05 #3
LAvoisieR wrote:
Following test code behaviour suprised me and I dont know what's wrong
with this. I have two overloaded constructors within base class and
virtual destructor implemented. Derived class uses protected
non-parametrized constructor of base class which DOES NOT initialize
private member 'value'. But by calling both destructors virtually,
binary does not complaint about deleting uninitialized pointer. WHY ??
C++ compiler used: G++ 3.4.3 #include <iostream> using namespace std;
class Base { public: Base(int) : value( new int(-14) ) {}
virtual ~Base() { //cout << *value << endl; // throws
SEGFAULT as supposed delete value; // no SEGFAULT
here ?? WHY ?? } protected: /* Constructor overload for
derived classes */ Base() { // does not
initialize 'value' pointer !!! } private: int *value;
}; class Derived : public Base { public: Derived() : Base() {}
virtual ~Derived() {} }; int main() { Base *p =
new Derived; delete p; }


Here's a sensible version of the code:

#include <iostream>
using namespace std;
class Base
{
public:
Base(int) : value( new int(-14) ) {}

virtual ~Base()
{
//cout << *value << endl; // throws SEGFAULT as supposed

delete value; // no SEGFAULT here ?? WHY ??
}
protected: /* Constructor overload for derived classes */
Base()
{ // does not initialize 'value' pointer !!!
}

private:
int *value;
};

class Derived : public Base
{
public:
Derived() : Base() {}
virtual ~Derived() {}
};

int main()
{
Base *p = new Derived;
delete p;
}

Methinks you just got lucky and that Base::value is 0. Note that it is
not guaranteed to be 0 (or anything else for that matter -- you should
initialize it to 0 in your default constructor). Dereferencing or
deleting an uninitialized pointer is bad bad bad.

Cheers! --M

Oct 27 '05 #4
On 2005-10-27, LAvoisieR <to****@volny.cz> wrote:
Following test code behaviour suprised me and I dont know
what's wrong with this. I have two overloaded constructors
within base class and virtual destructor implemented. Derived
class uses protected non-parametrized constructor of base class
which DOES NOT initialize private member 'value'. But by
calling both destructors virtually, binary does not complaint
about deleting uninitialized pointer. WHY ??


I took the liberty of reformatting your code. It can only have
been posted as it was by mistake. Please be more careful.

class Base
{
public:
Base(int) : value( new int(-14) ) {}
virtual ~Base()
{
delete value; /* no SEGFAULT here ?? WHY ?? */
}
protected: /* Constructor overload for derived classes */
Base() { } /* does not initialize 'value' pointer !!! */
private:
int *value;
};

class Derived : public Base
{
public:
Derived() : Base() {}
virtual ~Derived() {}
};

int main()
{
Base *p = new Derived;
delete p;
}

You can't count on getting a SEGFAULT. What happens when you try
to delete an uninitialized pointer is undefined. If you're lucky,
you get a crash of some sort. If you're unlucky, you just corrupt
the free store.

--
Neil Cerutti
Oct 27 '05 #5
LAvoisieR wrote:
Following test code behaviour suprised me and I dont know what's wrong
with this. I have two overloaded constructors within base class and
virtual destructor implemented. Derived class uses protected
non-parametrized constructor of base class which DOES NOT initialize
private member 'value'. But by calling both destructors virtually,
binary does not complaint about deleting uninitialized pointer. WHY ??
C++ compiler used: G++ 3.4.3
Please! Format your code next time.
#include <iostream>

using namespace std;

class Base
{
public:
Base(int)
: value( new int(-14) )
{
}

virtual ~Base()
{
// cout << *value << endl; // throws SEGFAULT as supposed
No, it's not supposed to "throw SEGFAULT", it's undefined behavior,
which means it could do anything.
delete value; // no SEGFAULT here ?? WHY ??
Because that's still undefined behavior. When I run your code on my
machine, it deletes all my emails while forcing me watch the little
paper clip in microsoft word dancing around.
}

protected:
/* Constructor overload for derived classes */
Base()
{
// does not initialize 'value' pointer !!!
Well, that's a bad idea.
}

private:
int *value;
};

class Derived : public Base
{
public:
Derived() : Base()
{
}
Unecessary:

Derived()
{
}

does the same thing.
virtual ~Derived()
{
}
};

int main()
{
Base *p = new Derived;
delete p;
}

Jonathan

Oct 27 '05 #6
in*****@gmail.com wrote:
LAvoisieR wrote:
Following test code behaviour suprised me and I dont know what's wrong

...
delete value; // no SEGFAULT here ?? WHY ??


Because it's ok to delete a NULL pointer.
It is not ok to dereference it.


If you hadn't snip all the code, you would have seen that it was
uninitialized, not null. Therefore, you get undefined behavior no
matter what you do with it, except giving it another value or doing
nothing.
Jonathan

Oct 27 '05 #7
Jonathan Mcdougall wrote:
[UB example redacted]

Because that's still undefined behavior. When I run your code on my
machine, it deletes all my emails while forcing me watch the little
paper clip in microsoft word dancing around.


You, sir, are truly evil. :) I can't get that image out of my mind now!
Oct 27 '05 #8
Thank you all for advices. I am still quite newbie in C++, I know. And
sorry about my mistake while sending message through web site. LaR

Oct 27 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jimmy Johns | last post by:
Hi all, I have some class hierarchy as follows: class S {}; class A {}; class B {public: vector<S*> v_; virtual ~B();}; class C : public virtual A, public virtual B { // do I need to define...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
20
by: qazmlp | last post by:
My class in a header file, contains inline virtual destructor. Is this Ok? Can it cause any problems? class base { public: base() { } virtual ~base { std::cout<<"Inside virtual destructor\n";...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
8
by: Daniel Yelland | last post by:
Hi, I have developed a number of code libraries in Win32 DLLs and have written a number of test suite executables that implicitly link to these libraries in order to test them. In one of my test...
7
by: dc | last post by:
Can anybody think of a situation where virtual function's address resolution/ something related to virtual is done at compile time
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
7
by: dragoncoder | last post by:
Hello experts, I was just playing around wrote this code. sundev1:/home/ptiwary/rnd $ cat a1.cpp #include <iostream> using namespace std; class Base
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.