Thread: Why sizeof() this struct is 12 ?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Why sizeof() this struct is 12 ?

    Code:
    struct st{
    	char c;
    	int i;
    	short s;
    };
    int main(){
    	cout<<sizeof(st)<<endl;
    }
    Why the size is 12, not 8 or 7?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    on my GCC/Linux 32-bit it's 8. I am guessing it's the compiler aligning addresses (adding padding to your struct).

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    In my MS Visual Studio it's 12.
    I can understand 8, but how come 12?

    Quote Originally Posted by cyberfish View Post
    on my GCC/Linux 32-bit it's 8. I am guessing it's the compiler aligning addresses (adding padding to your struct).

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I get 12.

    I suspect the c starts off on a 4-byte word boundary.

    Then, the int causes 3 pad bytes to be skipped in order to maintain a proper word boundary. That's 8 bytes so far.

    Then, the short gets 2 bytes, and the whole structure is most likely also padded out to a full word.
    Code:
    #include <stdio.h>
    #include <stddef.h> 
    
    struct st{
    	char c;
    	int i;
    	short s;
    };  
    
    int main(){
    	printf("&#37;d\n", sizeof(struct st) ) ; 
    	printf("%d\n", offsetof(struct st,c) ) ; 
    	printf("%d\n", offsetof(struct st,i) ) ; 
    	printf("%d\n", offsetof(struct st,s) ) ; 
    	return 0 ; 
    }
    Output:
    Code:
    [Session started at 2008-04-08 00:02:17 -0500.]
    12
    0
    4
    8
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The standard says the compiler can pad for any reason. Asking why the size is some particular value is fruitless, because the answer is officially, "Because."

  6. #6
    Banned
    Join Date
    Nov 2007
    Posts
    678
    try changing your struct to:
    Code:
    struct st
    {
        int i;
        short s;
        char c;
    };

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    An int (being 4 bytes long) needs to be aligned to 4 byte boundaries (at least for efficiency, and in some machines to avoid traps/faults):
    so your variable i is aligned to 4 bytes by padding 3 bytes after c. This makes 8 bytes so far.

    s is a short, so it takes up 2 bytes. This makes 10 bytes.

    But now we have the problem that if we make an array of struct st, the next instance of i will not be on an even boundary of 4, so the struct needs to be further aligned to match up with a int. This takes it out to 12 bytes. It would make no sense to align the int part to 4 bytes within the struct if an array of structs isn't also aligned such that the int part is aligned in every instance of such an array, right?

    As manav suggests, start with the biggest items, then smaller, and you won't get (as much) padding - it will still need to be aligned to match the biggest aligned item (4 bytes in this case), but the size now becomes 8.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. File processing program not workin...
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-30-2002, 11:25 PM
  5. Can't figure out why?
    By kwigibo in forum C Programming
    Replies: 10
    Last Post: 10-14-2001, 10:58 PM