[mpich-discuss] Problems with MPI and virtual functions in C++

Hélvio Vairinhos helvio.vairinhos at gmail.com
Wed Dec 12 14:00:28 CST 2012


Thanks for your suggestion, Dries! It works if I use &p.elem[0] as the 
initial address (not &p->elem), and only if I make elem public.I guess 
that's a price to pay (well, I can always fetch the address with a getter).

Do you know what happens to the memory layout in the presence of virtual 
functions if I have multiple components? For example:

     double a,b;

instead of:

     double elem[2];

Is it safe to assume that &p.a would give the initial address of a 
contiguous structure? Or should I play safe and use MPI_Pack and 
MPI_Unpack, for example?

Helvio



On 12/12/2012 07:37 PM, Dries Kimpe wrote:
> Classes/structs that have virtual functions have a different memory layout
> than structures/classes without them.
>
> In your code below, you're assuming that the class Pair has a memory
> layout of 2 doubles.
> This happens to be the case when there are no virtual functions in the
> class.
>
> However, if you add a virtual function, the memory layout changes.
> The class now also contains information about its runtime type
> (i.e. Pair).
>
> You can fix this by using &p->elem[0] as the address of the data you want
> to send/receive.
>
>    Dries




More information about the discuss mailing list