Results 1 to 6 of 6

Thread: How size of structure is defined

  1. #1
    Junior Member
    Join Date
    Apr 2007
    Answers
    1

    How size of structure is defined

    How size of structure is defined. If structure members are one character & one integer then size should be 5 bytes.but it is 8 bytes. Why it is so?


  2. #2
    Contributing Member
    Join Date
    Apr 2007
    Answers
    41

    Re: How size of structure is defined

    Memory is always allocated with respect to address allignment. I mean for 32 bit architecture the address are always 32bit alligned or 4byte alligned.
    So when declare a strcuture with one character and one one int variable first 4bytes will be allocated to make it alligned ie what we call padding.
    Try this declare a structure with 4 character variables, and one integer variable. the size of structure will be 8bytes.


  3. #3
    Junior Member
    Join Date
    Oct 2006
    Answers
    5

    Re: How size of structure is defined

    it'll alwyz show 8bytes..... but it'll use only 5bytes..


  4. #4
    Contributing Member
    Join Date
    Apr 2007
    Answers
    41

    Re: How size of structure is defined

    Yes even if your structure requires only 5bytes in order to make address alignment its padded with 3 more bytes.

    Why Padding?

    If the processor is serious about alignment it raises an exception/signal (ex. SIGBUS), otherwise there will be a performance penalty
    will be there as misalignment slowdown data access.

    So every time 4bytes if memory will be assigned irrespective of the data type.
    So even if u declare character in a structure 4bytes will be assigned.

    Hope its clear, else will answer in more detail


  5. #5
    Junior Member
    Join Date
    Feb 2006
    Answers
    8

    Re: How size of structure is defined

    The structure is packed. See #pragma pack for details.


  6. #6
    Junior Member
    Join Date
    Jun 2007
    Answers
    4

    Re: How size of structure is defined

    Hi,
    Hope it will give you clear information.
    while allocating memory to member variables, first size of member variable which occupies maximum, is determined.
    then allocation of memory, to each member variable goes in this block of size. below example will give more explaination.

    considering normally memory required for

    int = 4 bytes
    char = 1 bytes
    double = 8 bytes.

    #include <stdio.h>

    // Here memory allocated for member varaibles, is in terms of size of double(8 bytes).
    typedef struct st1
    {
    int k; // memory allocated is, memory bloack size 8 bytes, but actaully aquired by 'k' is 4 bytes.
    char c; // from previous memory block remaining 4 bytes, 1 byte is allocated for 'c', remaining 3 bytes padded.
    double m;// from remaming 3 bytes of prevoius memory block, it cannot allocate needed 8 bytes, so it acquires another memory block(ie 8 bytes).
    }myst1;

    // Here memory allocated for member varaibles, is in terms of size of double(8 bytes).
    typedef struct st2
    {
    int k; // memory allocated is, bloack size 8 bytes, but actaully aquired by 'k' is 4 bytes.
    double m; // from remaming 4 bytes of prevoius memory block, it cannot allocate needed 8 bytes, so it acquires another memory block ie 8 bytes.
    char c; // even though it needs only one byte, it has to be allocated in terms of memory block so it acquires another memory block ie 8 bytes.
    }myst2;

    void main()
    {
    myst1 str1;
    myst2 str2;
    int k= sizeof(str1);
    printf("\n structer size = %d \n",k);

    k= sizeof(str2);
    printf("\n structer size = %d \n",k);
    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact