Thread: From where these two extra bytes came?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    38

    From where these two extra bytes came?

    Please see following snap code. Sizes of individual members of structure are correct but when I print size of structure it gives two bytes extra.

    #define BYTE unsigned char
    #define INT16 unsigned short

    typedef struct Element
    {
    BYTE ID;
    BYTE Type;
    BYTE Ptr;
    INT16 Cnt;
    INT16 Idx;
    BYTE Status;
    };

    struct Element Element;


    main()
    {
    printf("Size of BYTE %d\n",sizeof(Element.ID));
    printf("Size of INT16 %d\n",sizeof(Element.Type));
    printf("Size of WORD %d\n",sizeof(Element.Ptr));
    printf("Size of DWORD %d\n",sizeof(Element.Cnt));
    printf("Size of DWORD %d\n",sizeof(Element.Idx));
    printf("Size of DWORD %d\n",sizeof(Element.Status));

    printf("Size of Element %d\n",sizeof(struct Element));

    }


    I am working in VC++ 6.0 and on NT platform(0x86).

    Size of struct element is 10, which is 2 bytes extra, if you add each member sizes. from where these two extra bytes came?

    Is Borland 4.0 also exbhits same way?

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    after the three bytes, the compiler needs to add one byte of waste before the int16s so that they will be aligned on 2 byte boundaries. similarly, after the end of the struct there is another 1 byte waste so that the next struct will begin on 2 byte boundary (beginning of struct is always aligned on the requirements of the strictest of its members).

    the offsetof() macro can tell you where in the struct each memeber starts.
    hello, internet!

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    38
    So you mean to say, If I rearrage the strcuture following way then it will give size as 8 only.

    typedef struct Element
    {
    BYTE ID;
    BYTE Type;
    BYTE Ptr;
    BYTE Status;
    INT16 Cnt;
    INT16 Idx;
    };

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    yep
    hello, internet!

  5. #5
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131
    Originally posted by moi
    after the three bytes, the compiler needs to add one byte of waste before the int16s so that they will be aligned on 2 byte boundaries. similarly, after the end of the struct there is another 1 byte waste so that the next struct will begin on 2 byte boundary (beginning of struct is always aligned on the requirements of the strictest of its members ).

    the offsetof() macro can tell you where in the struct each memeber starts.
    moi.. I have quoted your post with highlights which I really could understand.. which are..

    1. You have written next struct will begin on 2 byte boundary . I wanted to ask.. Is struct is a object of a struct or of any data structure....

    2. Secondly I couldnt understand ..what is strictest of structure members..

    explian... .?
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 03:17 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Shared class members over Dll Boundary
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 11-13-2007, 01:43 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM