Strcut padding?

Discussion in 'C++' started by webinfinite@gmail.com, Feb 23, 2008.

  1. Guest

    #include <iostream>
    #include <string>

    using namespace std;

    int main() {
    char t1;
    struct t2{
    char x;
    };

    cout << sizeof(t1) << " " << sizeof(t2) << endl;

    typedef char& x1;
    struct x2{
    x1 x;
    };

    cout << sizeof(x1) << " " << sizeof(x2) << endl;
    }

    output:
    1 1
    1 4

    Can anybody tell me why the size of x2 is 4? Why it's not 1?
    , Feb 23, 2008
    #1
    1. Advertising

  2. On Feb 22, 8:58 pm, "" <>
    wrote:
    > #include <iostream>
    > #include <string>
    >
    > using namespace std;
    >
    > int main() {
    >   char t1;
    >   struct t2{
    >     char x;
    >   };
    >
    >   cout << sizeof(t1) << "         " << sizeof(t2) << endl;
    >
    >   typedef char& x1;
    >   struct x2{
    >     x1 x;
    >   };
    >
    >   cout << sizeof(x1) << "         " << sizeof(x2) << endl;
    >
    > }
    >
    > output:
    > 1         1
    > 1         4
    >
    > Can anybody tell me why the size of x2 is 4? Why it's not 1?


    The following article should help:

    http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm

    --
    EventStudio 4.0 - http://www.Eventhelix.com/Eventstudio/
    Sequence diagram based systems engineering tool
    EventHelix.com, Feb 27, 2008
    #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. Dave
    Replies:
    7
    Views:
    5,639
    Joe Smith
    Jul 22, 2004
  2. RA
    Replies:
    1
    Views:
    359
  3. Becker

    Padding between textboxes

    Becker, Jun 24, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,256
    Eliyahu Goldin
    Jun 24, 2004
  4. Replies:
    0
    Views:
    432
  5. Mateusz_madi

    How to use maloc with strcut

    Mateusz_madi, May 13, 2010, in forum: C Programming
    Replies:
    2
    Views:
    802
    Mateusz_madi
    May 13, 2010
Loading...

Share This Page