sizeof struct

Discussion in 'C++' started by tom, Oct 24, 2006.

  1. tom

    tom Guest

    hiw is calculated size of struct


    for example:

    struct w1
    {
    char x1[252]; // 252
    __int64 x2; // 8
    // should be 260
    // is 264; WHY ??
    };
    // int DataSize = sizeof(w1);


    t.
    tom, Oct 24, 2006
    #1
    1. Advertising

  2. tom

    huangshan Guest

    "tom" <> wrote in message news:ehke3d$h9e$...
    > hiw is calculated size of struct
    >
    >
    > for example:
    >
    > struct w1
    > {
    > char x1[252]; // 252
    > __int64 x2; // 8
    > // should be 260
    > // is 264; WHY ??
    > };
    > // int DataSize = sizeof(w1);
    >
    >
    > t.
    >
    >


    i think 260 is right
    huangshan, Oct 24, 2006
    #2
    1. Advertising

  3. tom

    tom Guest

    I am using VC++ .NET

    t.
    tom, Oct 24, 2006
    #3
  4. tom

    huangshan Guest

    i see
    because __int64 x2; // 8

    so sizeof(w1)/8 must integer

    my englilsh poor,
    do you know what i want to say?
    huangshan, Oct 24, 2006
    #4
  5. tom wrote:

    > hiw is calculated size of struct
    >
    >
    > for example:
    >
    > struct w1
    > {
    > char x1[252]; // 252
    > __int64 x2; // 8
    > // should be 260
    > // is 264; WHY ??
    > };
    > // int DataSize = sizeof(w1);
    >


    Google for member alignment. Most probably yours is set to 8 bytes, so
    the x2 member will have an offset of 256 in your struct.

    Regards,
    Stuart
    Stuart Redmann, Oct 24, 2006
    #5
  6. tom

    Salt_Peter Guest

    tom wrote:
    > hiw is calculated size of struct
    >
    >
    > for example:
    >
    > struct w1
    > {
    > char x1[252]; // 252
    > __int64 x2; // 8
    > // should be 260
    > // is 264; WHY ??
    > };
    > // int DataSize = sizeof(w1);
    >
    >
    > t.


    Padding, 260/8 = 32.5 and 264/8 = 34
    for the same reason that a class like:

    struct C
    {
    char c;
    int n;
    };

    int main()
    {
    size_t size = sizeof(C);
    std::cout << "size = " << size << std::endl;

    return 0;
    }

    will also have a size matching the platform's memory indexing
    architecture (thankfully).
    Probably 8 but not neccessarily.
    Regardless, it shouldn't matter in your case, unless you are
    byte-copying the contents + padding around. Which you should not be
    doing anyways.
    Thats why operators and streams were created for.
    Salt_Peter, Oct 24, 2006
    #6
  7. tom

    Howard Guest

    "tom" <> wrote in message news:ehkeu2$jq7$...
    >I am using VC++ .NET
    >


    That's nice. Is there some reason you're telling us all what compiler
    you're using? Perhaps you're following up on a previous post? If so, then
    you should be quoting the post you're responding to.

    -Howard
    Howard, Oct 24, 2006
    #7
  8. tom

    Guest

    tom wrote:
    > hiw is calculated size of struct


    Size of members, plus size of padding. Padding is any memory used for
    internal
    compiler purposes. E.g. you'll often see a difference if a struct has
    virtual functions,
    but also if the members have mixed type.

    > for example:
    >
    > struct w1
    > {
    > char x1[252]; // 252
    > __int64 x2; // 8
    > // should be 260
    > // is 264; WHY ??
    > };


    Note that you have mixed types here.

    My guess in this case is that __int64 is a compiler-defined type that
    has
    alignment requirements. If it must start at an address that's a
    multiple of 8,
    then struct w1 must also. In an array of w1's, each w1 should. This in
    turn
    means the sizeof(w1) must be a multiple of 8.

    It may also be a speed optimization - again a 'internal compiler
    purpose'.

    HTH,
    Michiel Salters
    , Oct 25, 2006
    #8
    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. Derek
    Replies:
    7
    Views:
    24,323
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    626
    CBFalconer
    Apr 10, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,370
    Chris Fogelklou
    Apr 20, 2004
  4. Vinu
    Replies:
    13
    Views:
    1,409
    Lawrence Kirby
    May 12, 2005
  5. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    554
    Joe Smith
    May 22, 2006
Loading...

Share This Page