struct size
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: struct size

  1. #1
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    struct size

    though this is a simple qus im a bit confused.

    how many bytes are allocated for this structure?

    struct data
    {
    int i;
    char c;
    int j;
    };

    What if you use #pragma pack?.

    And please explain me #pragma pack, i didnt understood it from msdn.

    is it calculated as (int = 4bytes + char = 1byte + int = 4bytes) ?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: struct size

    The compiler is free to allocate as many bytes for a struct as it feels appropriate. If you use pragma pack then it should generate a size like you expect, but I'm not sure if that's guaranteed.

    Basically, computers access memory more efficiently on addresses which are multiples of 4 or 16. Some types in fact *require* this alignment. Depending on compiler settings, therefore, the fields of the struct may not be exactly adjacent in memory; padding for alignment may be inserted by the compiler. Praga pack instructs the compiler specifically *not* to do this.

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

    Re: struct size

    means normal declaration of

    int i;

    and

    struct a{
    int i;
    };

    differs in sizes ?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: struct size

    In that case probably not, but I don't know the standard well enough to say whether there are any guarantees.

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: struct size

    Try this

    Code:
    struct data{
       int i __attribute__ ((packed));
       char c;
       int j __attribute__ ((packed));
    };
    It's GNU specific, but then again, so is #pragma pack

  6. #6
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: struct size

    I just want to know how much bytes this struct will take, leave the pragma pack..

    struct data
    {
    int i;
    char c;
    int j;
    };

  7. #7
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: struct size

    Quote Originally Posted by hypheni View Post
    I just want to know how much bytes this struct will take
    Then use sizeof

  8. #8
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: struct size

    yes sizeof (data) returns 12bytes. But if I count it individually it should :

    4bytes + 1byte + 4bytes = 9bytes

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: struct size

    That's what I've been saying. The compiler is adding a few bytes to ensure the third field is on a 4-byte boundary for efficient access.

  10. #10
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: struct size

    So that means these extra bytes depending on compiler and for safe handling of memory ?

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: struct size

    Quote Originally Posted by hypheni View Post
    So that means these extra bytes depending on compiler and for safe handling of memory ?
    I'm not quite sure what you're asking here. The choice of struct padding is dependent upon the compiler and the compile options and may change if you build in a different environment.

  12. #12
    Join Date
    Jan 2009
    Posts
    1,689

    Re: struct size

    Te reason it does that is because most computers perform much better if your memory is WORD aligned. Usually a WORD is 4 bytes, so your compiler will expand and ever rearrange your struct to make that happen. You can stop the compiler from doing any rearranging by wrapping it in extern "C"{}, but even that will cause padding for alignment.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,427

    Re: struct size

    Quote Originally Posted by hypheni View Post
    yes sizeof (data) returns 12bytes. But if I count it individually it should :
    But it is not "individual" -- it is a struct, and a struct is one item. So to get the sizeof() a struct, you get sizeof(struct).

    Regards,

    Paul McKenzie

  14. #14
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: struct size

    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.

    one qus. does structure really defines its memory allocation apart from its content ?.

  15. #15
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,292

    Re: struct size

    Quote Originally Posted by ninja9578 View Post
    Te reason it does that is because most computers perform much better if your memory is WORD aligned. Usually a WORD is 4 bytes, so your compiler will expand and ever rearrange your struct to make that happen. You can stop the compiler from doing any rearranging by wrapping it in extern "C"{}, but even that will cause padding for alignment.
    This claim sparked my curiosity, as I was taught that elements in structs can't be re-arranged, ever, in C or C++. So I investigated a bit. I found these two interesting answers:

    http://stackoverflow.com/questions/9.../916691#916691
    http://stackoverflow.com/questions/1.../118177#118177

    Quote Originally Posted by C++ Section 9.2.12
    Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1)
    Quote Originally Posted by C99 Section 6.7.2.1
    Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared.
    Long story short, it can't.

    So yeah, that means the order of declaration can make a difference in the final struct size.

    Unless you are on a very space-bound system though, I wouldn't worry too much about it, and declare elements in an order that makes sense to a human operator. And respect things like add new elements at the bottom, this makes it easier for change tacking.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center