CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    118

    small Q on sizeof

    Dear Gurus, please refer the following code

    typedef struct _iphdr
    {
    unsigned int h_len:4;
    unsigned int version:4;
    unsigned char tos;
    unsigned short total_len;
    } IpHeader;
    when I check the size of the above structure,
    IpHeader m;
    int i = sizeof(m);
    system said "8", why? the first two int have taken 8 byte, plus 1 char, plus 2 byte short, it should be 11 byte.
    thanx a lot ahead.


  2. #2
    Join Date
    Oct 2000
    Posts
    379

    Re: small Q on sizeof

    Hi,
    the value of 8 results explains like this:
    your h_len and version struct members are bitfields of four bit each.
    They are packed in one byte, wasting three bytes of one integer where the whole bitfield is
    arranged in.

    typedef struct _iphdr
    {
    unsigned int h_len:4; //four LSbits, of least sign. byte of integer
    unsigned int version:4; //four MSbits, of least sign. byte of integer
    unsigned char tos; //two bytes, one byte wasted for "padding" (alignment)
    unsigned short total_len;//two bytes
    } IpHeader;




    Regards
    clem

    The simple logging/tracing control:
    http://www.softsyst.com/activelog_ef.htm


  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: small Q on sizeof

    h_len and version use 1 int (4 bytes)
    tos takes 1 byte
    total_len takes 2 bytes

    total: 7 bytes.

    BUT: total_len would be at an odd address (odd addresses are inefficient), so a "packing" byte gets inserted to push it to an even address - total 8 bytes.

    He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    118

    Re: small Q on sizeof

    Thank you so much for the answer, but according to the logic: the following struct:
    typedef struct tagSth{
    char ch1;
    char ch2;
    char ch3;
    }
    should take 4 byte, for you need padding, but the compiler still report 3 byte. According to my observation, only the odd char used with an even data (such as short) the padding happens. for example the follwing struct size is 4
    typedef struct tag sth {
    char ch;
    short s;
    }
    thank you for the answer again.


  5. #5
    Join Date
    Apr 2000
    Location
    Switzerland
    Posts
    944

    Re: small Q on sizeof

    The compiler does that right, no need to align 1 byte types on even addresses only - so you get 3 in the 1. example and the short in the second is then created at an even address, so you get 4.
    Don't argue with the compiler :-)



    The vast majority of our imports come from outside the country.
    George W. Bush

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