[Top] [All Lists]

Re: problem with multiple inheritance

Subject: Re: problem with multiple inheritance
From: Michael Mair
Date: Tue, 07 Mar 2006 06:59:55 +0100
PV schrieb:
Michael Mair wrote:
PV schrieb:

You invoke undefined behaviour by accessing the value of a pointer
pointing to deallocated storage. Seeming to "work just fine" is
possible just as well as "segfaulting".

The point is that I did not try to access the value of the pointer.
The line if( a == b) just compares the pointers, which are actually addresses in the memory. And even if the object b has been deallocated, it still points to some memory.

No. The value of the pointer is either
- a null pointer value; or
- the address of storage it points to; or
- "invalid"
The latter state comes from using an uninitialised pointer or
using a pointer which contains the address of some sort of freed
ressource (e.g. deallocated memory).
In the case of deallocated memory, the implementation may "outlaw"
all addresses which formerly belonged to the allocated memory, i.e.
the pointer value becomes a trap representation. The behaviour on
accessing a trap representation -- be it of a pointer, be it of a
floating point number or whatever else -- is undefined.

Per the C++ standard.

Maybe Ulrich Eckhardt's reply explains that better than mine -- the
gist is the same.

 > This has nothing to do with multiple inheritance.

I guess it does.
If you try another test program like the following, it will not segfault.

class A

class B : public A

int main()
    A* a  = new A;
    A* b  = new B;
    delete b;
    if( a == b )
    return 0;

Then you just got lucky in the former case and should now be
aware of your conceptual problem.
The undefined behaviour makes it seem to "work" here and gives
you a segfault there.

E-Mail: Mine is an   /at/ gmx /dot/ de   address.

<Prev in Thread] Current Thread [Next in Thread>