Re: class memory layout and portability

Discussion in 'C++' started by Gianni Mariani, Jul 7, 2003.

  1. Marco Jez wrote:
    > Hi programmers.
    > Let's say we have the following structures (suppose that Struct_A is defined
    > too):
    >
    > struct Struct_A;
    >
    > struct Struct_B {
    > Struct_A my_object;
    > void do_something_on_object(); // read and/or write some fields of
    > my_object
    > };
    >
    > Can I assert that instances of Struct_B will have the same memory layout and
    > size as Struct_A? So, is it safe/portable to do things like:
    >
    > Struct_A *obj_a = new Struct_A(.....);
    > Struct_B *obj_b = reinterpret_cast<Struct_B *>(obj_a);
    > obj_b->do_something_on_object();
    >


    In all practicality, yes if all methods are non-virtual. I'm not sure
    what the C++ standard says but the C standard I think (don't remember
    for sure) guarentees this.

    > What I'm trying to do is to enhance existing C-style structures by providing
    > a C++ interface around them, without creating new concrete data types by
    > encapsulation. I already know it works on my platform/system/compiler, but I
    > was wondering whether it should always work or it does just because I'm
    > lucky.


    It's likely to be one of those things that will work forever more. It's
    nigh impossible for a compiler to change the structure layout without
    causing all kinds of other problems.
    Gianni Mariani, Jul 7, 2003
    #1
    1. Advertising

  2. "Gianni Mariani" <> wrote...
    > Marco Jez wrote:
    > > Hi programmers.
    > > Let's say we have the following structures (suppose that Struct_A is

    defined
    > > too):
    > >
    > > struct Struct_A;
    > >
    > > struct Struct_B {
    > > Struct_A my_object;
    > > void do_something_on_object(); // read and/or write some fields of
    > > my_object
    > > };
    > >
    > > Can I assert that instances of Struct_B will have the same memory layout

    and
    > > size as Struct_A? So, is it safe/portable to do things like:
    > >
    > > Struct_A *obj_a = new Struct_A(.....);
    > > Struct_B *obj_b = reinterpret_cast<Struct_B *>(obj_a);
    > > obj_b->do_something_on_object();
    > >

    >
    > In all practicality, yes if all methods are non-virtual. I'm not sure
    > what the C++ standard says but the C standard I think (don't remember
    > for sure) guarentees this.


    I don't remember C standard allowing to have functions in structures....

    V
    Victor Bazarov, Jul 7, 2003
    #2
    1. Advertising

  3. Victor Bazarov wrote:

    >
    > I don't remember C standard allowing to have functions in structures....


    Vic,

    I was referring to the order of memory layout. Non virtual methods have
    no implication on layout of data.

    If you have a counter example, please do tell.

    I'm not saying behavior IS defined in the C++ standard, I'm saying that
    in practice, all relevant compiler implementations will do the same
    thing. This may be a hang-over from the cfront days.

    G
    Gianni Mariani, Jul 7, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Spiewak
    Replies:
    3
    Views:
    3,131
    Rick Spiewak
    Aug 26, 2003
  2. RobertH
    Replies:
    1
    Views:
    708
    Steve C. Orr [MVP, MCSD]
    Nov 4, 2003
  3. NWx
    Replies:
    4
    Views:
    2,949
    Kevin Spencer
    Feb 19, 2004
  4. Shane Beasley
    Replies:
    0
    Views:
    447
    Shane Beasley
    Jul 7, 2003
  5. Replies:
    1
    Views:
    561
    John Timney \(MVP\)
    Jun 19, 2006
Loading...

Share This Page