Thread: SIze of structure

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

    SIze of structure

    Hello,

    Here I have small program. When I print the size of individual elements of structe, I get correct answer. But when I print the size of entire structure it gives 10 instead of 8.

    This code developed using VC6.0

    Please let me know..

    Juganoo


    typedef unsigned char BYTE ; /* 8-bit data */
    typedef unsigned short INT16; /* 16 bit Data*/
    typedef unsigned int WORD ; /* 32-bit data */
    typedef double DWORD ; /* 64-bit data */

    typedef struct MacroElement
    {
    BYTE bElementID;
    BYTE bType;
    BYTE bPointer;
    INT16 iCount;
    INT16 iIndex;
    BYTE bStatus;
    };

    struct MacroElement MacroElement;


    main()
    {
    /*printf("Size of BYTE %d\n",sizeof(BYTE));
    printf("Size of INT16 %d\n",sizeof(INT16));
    printf("Size of WORD %d\n",sizeof(WORD));
    printf("Size of DWORD %d\n",sizeof(DWORD));*/

    printf("Size of BYTE %d\n",sizeof(MacroElement.bElementID));
    printf("Size of INT16 %d\n",sizeof(MacroElement.bType));
    printf("Size of WORD %d\n",sizeof(MacroElement.bPointer));
    printf("Size of DWORD %d\n",sizeof(MacroElement.iCount));
    printf("Size of DWORD %d\n",sizeof(MacroElement.iIndex));
    printf("Size of DWORD %d\n",sizeof(MacroElement.bStatus));

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

    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But when I print the size of entire structure it gives 10 instead of 8.
    Structures can have padding for alignment purposes.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    38
    I tried with setting member allignment to 1 byte and 2 byte. It is not accepting the command line option.

    Any suggestion.
    Juganoo

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Aligning is for your benefit, trust me.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    It depends, if you align everything on a 1 byte boundary you will save space but sacrafice speed. Likewise if you keep default boundaries i might access a bit faster but you'll lose some space for smaller data types. However, on desktop applications I agree there really aren't many reasons to pack structs tightly. If this was a palm pilot app then I would suggest it.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 07:45 PM