473,803 Members | 3,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1768
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.c z> 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.c om 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
1972
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 virtual ~C() so that B can be properly destructed?}; in fact, I don't even know if a destructor in C is even needed if I don't do
11
10526
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" is called in Case 2 since only ~base() becomes "virtual" and ~Derived() is still non-virtual? 3. Does Case 3 show that we don't need any virtual destructor to make ~Derived() called? 4. Is "virtual destructor" needed only for Case 2?
20
6000
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"; } // Other members
11
4372
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 it inline. Like this.
26
3989
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 add a virtual destructor like this : " virtual ~vechile". I get this error: Undefined first referenced symbol in file vtable for vechile /var/tmp//ccC9yD6Z.o
8
1539
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 applications, which runs fine in Debug mode, it is crashing in the destructor of a local object on the stack when it is built in release mode. An example of the C++ that causes the problem is as follows (apologies for the contrived example): -
7
2023
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
3093
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 item 7 for why this is virtual ...
7
2085
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
10546
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10068
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7603
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.