Thread: Structure and union size allocation

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    8

    Structure and union size allocation

    I am not sure about the sizes printed to this output ..............
    any reply is appreciated
    Thanks
    Code:
    /*E11_9*/
    #include<stdio.h>
    
    struct {
        char a[20];
        int b;
    
        union {
            double c;
    
            struct {
                char d[15];
                float e;
            } x;
        } y;
    } z;
    
    int main(void) {
        printf("%u\n",sizeof (z));
        return 0;
    }
    outupu:20 24 28

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    There is only one printf call, and it only prints one value, so I am not sure how you get three values printed.

    Anyway, you need to tell us what problems you have interpreting or understanding the code and/or output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    8
    sry printf("%u%u%u\n",sizeof(z),sizeof(y),sizeof(x));

  4. #4
    Registered User
    Join Date
    Jan 2015
    Posts
    8
    so the above is the modification

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Alright, this is the only way I could get your code to compile silently under
    Code:
    gcc -std=c11 -Wall -Wextra -pedantic -O3
    Code:
    #include <stdio.h>
    
    
    struct z
    {
        char a[20];
        int  b;
    
    
        union
        {
            double c;
    
    
            struct
            {
                char d[15];
                float e;
            } x;
        } y;
    };
    
    
    int main(void)
    {
        struct z test;
    
    
        printf("sizeof(z) == %zd\n", sizeof(test));
        printf("sizeof(y) == %zd\n", sizeof(test.y));
        printf("sizeof(x) == %zd\n", sizeof(test.y.x));
    
    
        return 0;
    }
    Output :
    Code:
    sizeof(z) == 48
    sizeof(y) == 24
    sizeof(x) == 20
    Basically, the compiler is doing automatic alignment for these types.

    For x, 15 + 4 = 19 which would align to 20.

    As for why y is 24, I'd only image because you have 20 and 4 so for the compiler to properly align that, it uses 24 bytes.

    And as for z, it takes that previous 24 and then adds the other 20 + 4 bytes so you wind up with 48.

    Read up on memory alignment and stuff like that.

    compiler construction - Why do we have alignment padding if memory is byte-addressable? - Stack Overflow

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Actually, here's some updated code that shows the offsets from the base of the structure.
    Code:
    #include <stdio.h>
    #include <stddef.h>
    
    
    struct z
    {
        char a[20];
        int  b;
    
    
        union
        {
            double c;
    
    
            struct
            {
                char d[15];
                float e;
            } x;
        } y;
    };
    
    
    int main(void)
    {
        struct z test;
    
    
        printf("sizeof(z) == %zd\n", sizeof(test));
        printf("sizeof(y) == %zd\n", sizeof(test.y));
        printf("sizeof(x) == %zd\n", sizeof(test.y.x));
    
    
        ptrdiff_t base = (ptrdiff_t ) &test;
    
    
        ptrdiff_t a = (ptrdiff_t ) &(test.a);
        ptrdiff_t b = (ptrdiff_t ) &(test.b);
    
    
        ptrdiff_t c = (ptrdiff_t ) &(test.y.c);
    
    
        ptrdiff_t d = (ptrdiff_t ) &(test.y.x.d);
        ptrdiff_t e = (ptrdiff_t ) &(test.y.x.e);
    
    
        printf("a - base == %td\n", a - base);
        printf("b - base == %td\n", b - base);
        printf("c - base == %td\n", c - base);
        printf("d - base == %td\n", d - base);
        printf("e - base == %td\n", e - base);
    
    
        return 0;
    }
    Output :
    Code:
    sizeof(z) == 48
    sizeof(y) == 24
    sizeof(x) == 20
    a - base == 0  // a starts at start of structure
    b - base == 20 // b begins 20 bytes after
    c - base == 24 // c starts where it should
    d - base == 24 // d also starts where it should
    e - base == 40 // then, 16 bytes later, we have e (15 would be aligned to 16)
    As for why the total is 48 and not 44, I'm not sure considering most floats are 4 bytes. Again, probably weird compiler alignment but this sort of shows what's going on.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by MutantJohn View Post
    As for why the total is 48 and not 44, I'm not sure considering most floats are 4 bytes. Again, probably weird compiler alignment but this sort of shows what's going on.
    Since sizeof(y) is 24, the only way the total could be 44 would be if an int (member named b) had size zero. Which is not possible.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Size of Union
    By Mahesha Padyana in forum C Programming
    Replies: 2
    Last Post: 01-19-2013, 12:13 AM
  2. size of union without using sizeof operator
    By shruthi in forum C Programming
    Replies: 20
    Last Post: 08-31-2012, 06:34 AM
  3. union will support dynamic memory allocation?
    By nkrao123@gmail. in forum C Programming
    Replies: 8
    Last Post: 11-24-2011, 09:56 AM
  4. Memory allocation in an UNION
    By NKP in forum C Programming
    Replies: 6
    Last Post: 06-13-2010, 10:58 PM
  5. Size of the UNION
    By BlackOps in forum C Programming
    Replies: 14
    Last Post: 07-11-2009, 04:29 PM