-1
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?

4
Contributors
5
Replies
23
Views
4 Years
Discussion Span
Last Post by saurabh.mehta.33234
0

That rather depends on the size of int on your computer.

If size of int is 2 and size of long is 4 then in your struct ss there is likely to be 2 padding bytes in the structure between c and d. These bytes are never assign but it is these bytes that are shared with the b in struct s.

This is all to do with the size of basic types on your platform and the your platforms structure packing algorithm.

trying outputing

sizeof(int)
sizeof(long)
sizeof(struct s)
sizeof(struct ss)

Edited by Banfa

-1

@banfa ohhhk.. So the value 32767 is a value by default added by compiler into padding byte or is it something different??Sizeof int is 4 and sizeof long is 8 and sizof struct ss is 16..The padding is of 4 bytes..But how does the 32767 come..?

0

But how does the 32767 come..?

It's made of whatever random values happen to be in that memory. You're reading memory you never set to a value, so it could have anything in. If your code above is the whole program, it's just whatever was left in that memory by whatever set it last.

Edited by Moschops

1

You're invoking undefined behavior, the output could be anything (notably gcc will produce different output with -O1 than with -O2). It is highly unlikely that your compiler writes a specific byte sequence into bytes used for padding. Rather I'd expect it simply leaves them untouched, so they will just have whatever value previously resided in that memory location. If you move your code into a function, you'll probably notice that the value of b will change depending on whether or not you call other functions before calling your function.

For example the following code, compiled with gcc without optimizations, will produce different results on my system depending on whether or not I comment out the call to g():

#include <stdio.h>

void f()
{       
    union u
    {   
        struct s{int a;int b}n;
        struct ss{int c;long d}ni;
    } uu;
    uu.ni.c=42;
    uu.ni.d=23;
    printf("%d %d",(uu.n.a),(uu.n.b));
}

void g() {
    int x=1, y=2, z=3, a=4;
    printf("%d %d %d %d\n", x, y, z, a);
}

int main() {
    g();
    f();
    return 0;
}

When I compile without optimizations and call g, b will have the same value that z had in g -- changing z will change the value of b. When compiled with optimizations, b will always be 0 -- whether I call g or not.

-1

@sepp2k great explaination!!!Doubt cleared and i suppose as per your code b will show value in y and not z..nevermind..good concept..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.