Thread: C structure size

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    1

    C structure size

    Hello Everyone
    Someone told me its never too late to start diving into the world of C programming. So here I am.

    I am trying to find the difference between Struct and Union using the sizeof() function. I realise int maybe 2 or 4 bytes depending on the processor, char 1 byte, float 4 byte.

    Code:
    
    
    Code:
    struct student
    {
     char name[30];
     int num;
     float fnum;
    };
    
    union teacher
    {
     char name[30];
     int num;
     float fnum;
    };
    int main()
    {
     struct student data;
     union teacher details;
     printf("The size of charx30 ,intx1 and floatx1 are %d,%d,%d respectively \n",sizeof(data.name),sizeof(data.num),sizeof(data.fnum));
     printf("The size of struct data is %d\n",(sizeof(data))); 
     printf("The size of union teacher is %d\n",(sizeof(details)));  
     return 0;
    }
    Output:
    The size of charx30 ,intx1 and floatx1 are 30,4,4 respectively
    The size of struct data is 40
    The size of union teacher is 32

    my question is why is it 40 and 32 instead of 38 and 30??
    30+4+4=38.


    What am I missing??
    Thank you for any response.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well remember the extra 2 for the struct is probably because it is easier to sit the array in a 32 byte block. The union is in a similar situation, but it's smaller because you can technically store only one of the members at a time.

    All data types like this can have optional padding.
    Last edited by whiteflags; 07-18-2017 at 03:09 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Data structure alignment - Wikipedia

    Modern CPU architectures are more efficient if wide data types like int are aligned on specific address boundaries. As a rough rule of thumb, the alignment requirement of a data type is the same as it's size. So for example, a 4-byte int needs to be at an address which is a multiple of 4.

    A consequence of this is that structs are usually padded to be a multiple of whatever the alignment is of the widest member.
    Such that if you have arrays of your struct, the wide members will always be at a correctly aligned address.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure and union size allocation
    By richard36 in forum C Programming
    Replies: 6
    Last Post: 01-22-2015, 05:44 AM
  2. Puzzled about the size of my data structure
    By jaarestad in forum C Programming
    Replies: 7
    Last Post: 03-28-2013, 06:09 AM
  3. size of structure
    By Leone in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2010, 08:56 AM
  4. Size of structure in bytes
    By Micko in forum C Programming
    Replies: 2
    Last Post: 11-29-2004, 10:09 AM
  5. SIze of structure
    By Juganoo in forum C Programming
    Replies: 4
    Last Post: 12-03-2002, 10:23 PM

Tags for this Thread