allignment of structure

Discussion in 'C++' started by Anthony, Nov 11, 2003.

  1. Anthony

    Anthony Guest

    Hi,
    I've got a question about the allignment of data in a structure.
    What happens in memory when i declare the following

    struct A
    {
    int x;
    }
    stuct B : public A
    {
    int y;
    }

    Is it compiler specific how the memory is alligned when I instance B?
    Or can do something like this:

    B b;
    int* z = &(reinterpret_cast<int>(b)); //z point to x
    z++; //z points to y

    Or does the VTABLE pointer cause problems here?

    Thanks in regard,
    Anthony Lansbergen
    Anthony, Nov 11, 2003
    #1
    1. Advertising

  2. "Anthony" <-is-A.A-is-E.nl> wrote...
    > I've got a question about the allignment of data in a structure.
    > What happens in memory when i declare the following
    >
    > struct A
    > {
    > int x;
    > }
    > stuct B : public A
    > {
    > int y;
    > }
    >
    > Is it compiler specific how the memory is alligned when I instance B?


    Yes.

    > Or can do something like this:
    >
    > B b;
    > int* z = &(reinterpret_cast<int>(b)); //z point to x
    > z++; //z points to y


    Most likely you can't. The Standard imposes no requirement on
    the behaviour of such code. It's called "undefined behaviour".

    >
    > Or does the VTABLE pointer cause problems here?


    There is no such thing in the language as "VTABLE pointer". If
    you're talking of a very common way to implement virtual function
    mechanism in the language, then what does it matter here? Your
    structs have no virtual functions.

    Victor
    Victor Bazarov, Nov 11, 2003
    #2
    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. Aitham alama

    Center allignment

    Aitham alama, Jul 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    334
    John Saunders
    Jul 18, 2004
  2. EB
    Replies:
    7
    Views:
    492
    Dale King
    Feb 16, 2004
  3. James

    allignment

    James, Mar 24, 2006, in forum: HTML
    Replies:
    5
    Views:
    512
    dorayme
    Mar 25, 2006
  4. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    739
    Martin Ambuhl
    Oct 26, 2004
  5. MisterE

    Memory allignment/type casting question

    MisterE, Oct 8, 2008, in forum: C Programming
    Replies:
    6
    Views:
    383
    CBFalconer
    Oct 9, 2008
Loading...

Share This Page