Thread: unions

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    50

    unions

    Code:
    main()
    {
    union u
    {
    struct s{int a;int b}n;
    struct ss{int c;long d}ni;
    
    
    }uu;
    uu.ni.c=1;
    uu.ni.d=0;
    
    
    
    
       printf("%d %d",(uu.n.a),(uu.n.b));
    }
    output
    1
    32767

    Now my question is that even though I put d=0 then why does it shows 32767 as output for b?As per my knowledge union members share the same place of memory..Then why is this hapenning here?

  2. #2
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    When you assign a value for one member of a union, you override the value(s) stored in the other member(s). The other values are no longer valid.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    Yeah that i know..in my case it means wirting to c and d means overiding the values of a and b...But why does that 32767 come??

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is probably a consequence of your compiler using ones-complement representation of signed integral types.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    Quote Originally Posted by grumpy View Post
    It is probably a consequence of your compiler using ones-complement representation of signed integral types.
    I think that its because of padding that extra bits are added..Because if i do sizeof(struct ss) it gives 16 and not 12...Please contribute if I am wrong?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Possibly. Possibly not.

    Whatever the cause, your code exhibits undefined behaviour. Any results you get are therefore technically correct, according to the standard. If someone found that your code triggered segmentation faults, that would also be technically correct. The reasons come down to how your compiler is implemented.

    In a C forum, the actual reason for the behaviour does not matter. To a practical C developer, the only conclusion of interest is that they should not be using unions in the manner that you are.
    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.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    @grumpy thank you for your valuable contribution..

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > As per my knowledge union members share the same place of memory
    By the time you've taking into padding and alignment, the amount of overlap between any two apparently adjacent variables might be zero.

    Which is seems to be in this case.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    int main()
    {
      union u {
        struct s {
          int a;
          int b;
        } n;
        struct ss {
          int c;
          long d;
        } ni;
      } uu;
      uu.ni.c = 1;
      uu.ni.d = 0;
      printf("%d %d\n", (uu.n.a), (uu.n.b));
      printf("n.a is at %ld\nn.b is at %ld\nni.d is at %ld\n",
             offsetof(union u,n.a), offsetof(union u,n.b), offsetof(union u,ni.d) );
      return 0;
    }
    
    
    $ ./a.out 
    1 32767
    n.a is at 0
    n.b is at 4
    ni.d is at 8
    The assignment of ni.d completely misses all the bytes where n.b would reside, so all you see is random garbage.

    The a and c members, by virtue of being the first member in their respective structs, AND of the same type, can be freely assigned in one and inspected in the other with no problem at all.
    But this is a unique special case.
    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. unions
    By Saurabh Mehta in forum C Programming
    Replies: 1
    Last Post: 04-04-2013, 11:51 PM
  2. unions
    By jduke44 in forum C Programming
    Replies: 2
    Last Post: 10-26-2005, 03:50 PM
  3. Unions?
    By Sure in forum C Programming
    Replies: 8
    Last Post: 06-30-2005, 02:47 AM
  4. Unions in c++
    By The Gweech in forum C++ Programming
    Replies: 5
    Last Post: 08-06-2002, 08:14 AM
  5. Unions?
    By hostensteffa in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2002, 06:01 AM

Tags for this Thread