CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24

Thread: struct size

  1. #16
    Join Date
    Apr 1999
    Posts
    27,449

    Re: struct size

    Quote Originally Posted by hypheni View Post
    Yes that I posted earlier that if I use sizeof(struct) is gives 12. but considering the struct contents value its not being 12, thats why asked.
    A structs contents is not only what you see visibly -- that's the point. The contents of the struct include padding bytes which you can't see or predict by just looking at a struct declaration.

    Regards,

    Paul McKenzie

  2. #17
    Join Date
    Jan 2009
    Posts
    1,689

    Re: struct size

    My mistake, I thought the compiler would shift items to word align them in C++ :/

  3. #18
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: struct size

    Thanks Paul. You cleared my doubt. But is there any trick to guess struct size.

  4. #19
    Join Date
    Jan 2009
    Posts
    1,689

    Re: struct size

    sizeof() as been suggested many times.

    You can use this rule of thumb though. Take the number of bytes you expect it to be, and round up to the next multiple of four. That's usually a pretty good guess, but it's only a guess. No promises.

    You should never ever require the size of it (one exception I can think of,) because programmatically, you will use sizeof() and things like array indexing and offsets are calculated automatically (and at compile time of course)

  5. #20
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: struct size

    Yes if I need to know this programmatically then sizeof is okay.

    but if this qus is asked me in some written exam ?. actually the main posted qus is taken from some interview qus series.

  6. #21
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: struct size

    Quote Originally Posted by hypheni View Post
    but if this qus is asked me in some written exam ?. actually the main posted qus is taken from some interview qus series.
    The correct answer on a written exam would be to state that the struct size is dependent on compiler choice and compile options. If you wish you can state one possible size by rounding every field's size up to the nearest multiple of 4 and then adding, but be clear that's just a possibility, not a certain size.

    If the question asks about pragma pack, then you can specify an exact size by using the alignment rules laid out here:
    http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx

  7. #22
    Join Date
    Jan 2009
    Posts
    1,689

    Re: struct size

    Quote Originally Posted by Lindley View Post
    rounding every field's size up to the nearest multiple of 4 and then adding
    I don't think this is correct.

    Code:
    struct foo {
       short i;
       short j;
    };
    Should be 4 bytes on most platforms (is on OSX with GCC.) You actually want to add them all up first, then round. Not sure if you just mistyped or not, but thought I would clear it up for the OP. But again, this is a guess at the size.

  8. #23
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: struct size

    Quote Originally Posted by ninja9578 View Post
    I don't think this is correct.

    Code:
    struct foo {
       short i;
       short j;
    };
    Should be 4 bytes on most platforms (is on OSX with GCC.) You actually want to add them all up first, then round. Not sure if you just mistyped or not, but thought I would clear it up for the OP. But again, this is a guess at the size.
    The real rules are more complicated than what either of us suggested, but both will serve as "reasonable guesses" if you're trying to predict such things without a compiler in front of you.

  9. #24
    Join Date
    Jan 2009
    Posts
    1,689

    Re: struct size

    I imagine when getting into unions or bit fields the rules start getting really weird.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured