virtual table and shared memory

B

Bernard

Hi,
Is there a way to have the virtual table of c++ objects allocated in shared
memory (in fact in the same address space as the object is) instead of
having it allocated in the process address space ?

This could be useful to share objects between processes without having to
call the constructor of the object twice (I think it is the constructor that
is creating the virtual table, right ?)

Thanks
 
I

Ian

Bernard said:
Hi,
Is there a way to have the virtual table of c++ objects allocated in shared
memory (in fact in the same address space as the object is) instead of
having it allocated in the process address space ?
Sorry, no.

Ian
 
I

Ian

Bernard said:
This could be useful to share objects between processes without having to
call the constructor of the object twice (I think it is the constructor that
is creating the virtual table, right ?)
Forgot to say, you just plain can't share objects with virtual methods
via shared memory.

Ian
 
B

Bernard

Well I think I can:

process A create object O:
O* o = new(shared_memory_address) O(my_init);

process B use object O
O* o = new(o) O(no_init_of_attribute); // Constructor of O called,
virtual-table is created again

This seems to work with g++. Wondering if I could avoid the 2nd call of the
constructor
 
M

msalters

Bernard schreef:
Hi,
Is there a way to have the virtual table of c++ objects allocated in shared
memory (in fact in the same address space as the object is) instead of
having it allocated in the process address space ?

vtables are just one way to implement virtual functions, and ISO C++
doesn't
even have shared memory, or address spaces. Your question therefore is
off-topic here, ask in your OS/compiler group.

(In general, you wouldn't want it either. If you've got two copies of a
function in two different address spaces, on different addresses, you'd
also want two tables of function pointers. Plus, you'd want two vtable
pointers in each object, and some way of knowing which one to use. E.g.
it really doesn't work nicely. Plus there's RTTI which often is done
via the vtable, too)
 
H

Harish

Bernard said:
Well I think I can:

process A create object O:
O* o = new(shared_memory_address) O(my_init);

process B use object O
O* o = new(o) O(no_init_of_attribute); // Constructor of O called,
virtual-table is created again
How about casting the shared_memory_address in process B to O*? wouldn't
that work?
alsom its not just the ctor, u dont want a call to dtor in process B
using type cast or reinterpret_cast, is more safe?
This seems to work with g++. Wondering if I could avoid the 2nd call of
the constructor
I think there is no virtual table per object....virtual table is a class
variable.
all the obejcts of the same class shares/uses the same vtable?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top