Prev: ADL woes
Next: -1%N = -1
From: courpron on
On Apr 19, 1:16 am, xtrigger...(a)gmail.com wrote:
> Thanks a lot to all.
>
> I got Lance point.
> But I just want to get the address of the first enclosing object.
> I'm not interested in the dynamic type of an object that may have
> derived from this "first encloser".
> ( Hope my english is good enough ).


If you don't care at all about the dynamic type of the object, then it
may work in practice.


> PLUS, I do not understand why this technique should not work with non
> POD types.

It ultimately depends on the compiler. For example, popular
implementations of multiple and virtual inheritance involve member
reordering. Consider the following example :


#include <cassert>
#include <cstddef>

struct COuter {
char mPad1[ 5 ];
int mInner;
char mPad2[ 5 ];
};

struct Base : public virtual COuter {
int b;
};

struct Derived :public Base, public virtual COuter {
int d;
};

void* GetEnclAddrFromInner ( int * inner )
{
return (void*) ( (char*)inner -
offsetof(COuter, mInner) );
}


void CheckPointers ( void* true_ptr, COuter * obj ) {
assert( true_ptr == GetEnclAddrFromInner( &obj->mInner ) );
}

int main()
{
COuter a;
Derived b;

CheckPointers( (void*)&a, &a ); #1
CheckPointers( (void*)&b, &b ); #2
}

Line #1 will probably be ok, while line #2 probably won't pass the
assertion, depending on the compiler.


Alexandre Courpron.



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: courpron on
On Apr 21, 6:26 pm, xtrigger...(a)gmail.com wrote:
> Hi and thanks again for answering.
> You gents have convinced me is not the path to follow.
> I'm proceeding in a different direction.
> But by looking at Alexandre's example I thing you've misunderstood my
> intentions.

I think I understood your intentions. My example is merely
illustrating the member reordering side-effects.

I said that if you don't care at all about the dynamic type of the
object your are working on [ that is to say if, for the latter, static
type == dynamic type ], then it may work in practice, although such
behavior is not described explicitly by the C++ standard.

In your first message, you didn't state that you would always work on
non-polymorphic objects, so we were saying that it is not reliable to
use the offset of a member of a non-POD class. Then I explained this
*original* statement with the example of the member reordering part of
some implementations of multiple and virtual inheritance, followed by
a code example.


> And I'm confused by the statement that compilers might rearrange
> members...

It would be pretty long to explain how a compiler could implement
multiple and virtual inheritance. I found a link that explains pretty
clearly such implementation :

http://www.phpcompiler.org/doc/virtualinheritance.html

>
> Memory layout examples [ ... ]
>

In your examples, you want the address of the sub-object "A" which is
of course always non-polymorphic. If you wanted the address of an
object such as : A* object = new C();
then getting it from the offset of a member in "A" wouldn't have been
reliable. Since, you stated in your second post that it won't be the
case, then I answered "it may work in practice (c)".


Alexandre Courpron.



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

First  |  Prev  | 
Pages: 1 2
Prev: ADL woes
Next: -1%N = -1