public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* When C++ obeject is passed to C function
@ 2002-08-06  8:32 VallabhaN
  2002-08-06  8:46 ` Eljay Love-Jensen
  0 siblings, 1 reply; 4+ messages in thread
From: VallabhaN @ 2002-08-06  8:32 UTC (permalink / raw)
  To: gcc-help

Hello All,

     I have come across a situation where I need to map a C structure of
function pointers to C++ virtual table pointers. Using padding I able to map
this. Now my problem is inspite of proper mapping the sample is dumping core
when tried to call the function. Here is the gdb output which depicts more
clearly whats happening

===========================================================================================================
(gdb) run
Starting program: /reference/vtable/linux_ia64-g/vtable

Breakpoint 1, main () at cppfile.cxx:8
8               myDerived a;
(gdb) n
11              a.Release( );
(gdb) n
in Release
12              a.f1(g);
(gdb) n
into f1
13              a.f2("dkfj");
(gdb) n
into f2, dkfj
14              call_cplusplus(&a);
(gdb) s
call_cplusplus (p=0x80000fffffffb4c0) at cfile.c:11
11        p->lpVtbl->AddRef(p);
Current language:  auto; currently c
(gdb) p * p->lpVtbl
$1 = {QueryInterface = 0x4000000000001150 <myDerived::QueryInterface(_GUID
const&, void**)>, wupad1 = 6917529027641088424,
  AddRef = 0x40000000000011e0 <myDerived::AddRef()>, wupad2 =
6917529027641088424,
  Release = 0x4000000000000f40 <myDerived::Release()>, wupad3 =
6917529027641088424,
  f1 = 0x4000000000000fb0 <myDerived::f1(_GUID const&)>, wupad4 =
6917529027641088424,
  f2 = 0x4000000000001030 <myDerived::f2(char*)>, wupad5 = 6917529027641088424,
  f3 = 0x4000000000001250 <myDerived::f3(unsigned long)>, wupad6 =
6917529027641088424,
  f4 = 0x40000000000012e0 <myDerived::f4(unsigned short, unsigned short)>}
(gdb) p p->lpVtbl->AddRef(p)
in AddRef
$2 = 0
(gdb) n
11        p->lpVtbl->AddRef(p);
(gdb) n

Program received signal SIGILL, Illegal instruction.
0x5800a191800 in ?? ()
(gdb)

==============================================================================================================

     Can somebody tell me whats going wrong or am I missing something??
     Any help will be really appreciated.

Thanks in advance
-Vallabha


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: When C++ obeject is passed to C function
  2002-08-06  8:32 When C++ obeject is passed to C function VallabhaN
@ 2002-08-06  8:46 ` Eljay Love-Jensen
  0 siblings, 0 replies; 4+ messages in thread
From: Eljay Love-Jensen @ 2002-08-06  8:46 UTC (permalink / raw)
  To: VallabhaN, gcc-help

Hi Vallabha,

Don't try to put instance method virtual function pointers into a C 
structure of function pointers.  An instance method virtual function 
pointer is a different animal.

You should use glue/stub routines.

class Base {
public:
    // Option #1 entry stub...
    static void KickEntry(void* self) {
       Base* base = static_cast<Base*>(self);
       base->KickSelf();
    }
    // ...end-of-#1.

    // Option #2 entry stub...
    /*final*/ void Kick() { KickSelf(); }
    // ... end-of-#2.

    virtual void KickSelf() = 0;
};

Put KickEntry() or Kick() into your C structure of function 
pointers.  KickSelf() won't work.

My preference is KickEntry(), but if you are careful Kick() will work too.

Also, strongly consider making KickSelf() private.  Or at least, protected.

--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: When C++ obeject is passed to C function
  2002-08-06  9:25 VallabhaN
@ 2002-08-06  9:53 ` Eljay Love-Jensen
  0 siblings, 0 replies; 4+ messages in thread
From: Eljay Love-Jensen @ 2002-08-06  9:53 UTC (permalink / raw)
  To: VallabhaN; +Cc: gcc-help

Hi Vallabha,

Your working sample is brittle, in the sense that it is very dependent upon 
the platform (compiler + OS) implementation details.

My suggestion is a means to allow you to avoid platform dependencies and 
intimate knowledge thereof.

Your alternative would be to become familiar with the underlying 
implementation on 64-bit GCC 3.1, and have #if/#endif blocks in your code 
to handle the differences (or use some other proxy pattern / abstraction 
layer to avoid code "pollution").

I'm not familiar with the underlying implementation schema used on any 
64-bit GCC 3.1.

--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: When C++ obeject is passed to C function
@ 2002-08-06  9:25 VallabhaN
  2002-08-06  9:53 ` Eljay Love-Jensen
  0 siblings, 1 reply; 4+ messages in thread
From: VallabhaN @ 2002-08-06  9:25 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: gcc-help

Hi Eljay,

     Thanks for your reply.  I have the same sample working fine on 32 bit m/c
with gcc 2.96. Is this the problem specific to 64 bit gcc 3.1 ??
Thanks again
-Vallabha



To:   VallabhaN/Bristol Technology@Bristol Technology, gcc-help@gcc.gnu.org
cc:

Subject:  Re: When C++ obeject is passed to C function




Hi Vallabha,

Don't try to put instance method virtual function pointers into a C
structure of function pointers.  An instance method virtual function
pointer is a different animal.

You should use glue/stub routines.

class Base {
public:
    // Option #1 entry stub...
    static void KickEntry(void* self) {
       Base* base = static_cast<Base*>(self);
       base->KickSelf();
    }
    // ...end-of-#1.

    // Option #2 entry stub...
    /*final*/ void Kick() { KickSelf(); }
    // ... end-of-#2.

    virtual void KickSelf() = 0;
};

Put KickEntry() or Kick() into your C structure of function
pointers.  KickSelf() won't work.

My preference is KickEntry(), but if you are careful Kick() will work too.

Also, strongly consider making KickSelf() private.  Or at least, protected.

--Eljay





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-08-06 16:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-06  8:32 When C++ obeject is passed to C function VallabhaN
2002-08-06  8:46 ` Eljay Love-Jensen
2002-08-06  9:25 VallabhaN
2002-08-06  9:53 ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).