Strange behaviour - Unions.

S

siddharth.munshi

union something
{
float a;
int b;
} u;

int main()
{
printf("%d %d\n",&u.a,&u.b);
u.a = 200;
printf("%f %d \n",u.a,u.b);
return 0;
}


The above code prints:

134518260 134518260
200.00000 1128792064

While the first line of output explains itself. I want to know what is
the relation between the value that we set for 'a', and the value that
'b' returns.
 
M

Mike Wahler

union something
{
float a;
int b;
} u;

int main()
{
printf("%d %d\n",&u.a,&u.b);
u.a = 200;
printf("%f %d \n",u.a,u.b);

Here, the expression 'u.b' gives undefined behavior,
since it was not the last union member assigned
return 0;
}


The above code prints:

134518260 134518260
200.00000 1128792064

Or it could have printed something else. Or nothing at all.
Or it could have done something completely arbitrary. This
is the nature of undefined behavior.
While the first line of output explains itself. I want to know what is
the relation between the value that we set for 'a', and the value that
'b' returns.

None whatsoever. In your program, 'u.b' has no defined value.

What are you really trying to do? Why do you think you need
a union?

-Mike
 
S

Spiros Bousbouras

union something
{
float a;
int b;
} u;

int main()
{
printf("%d %d\n",&u.a,&u.b);
u.a = 200;
printf("%f %d \n",u.a,u.b);
return 0;
}


The above code prints:

134518260 134518260
200.00000 1128792064

While the first line of output explains itself. I want to know what is
the relation between the value that we set for 'a', and the value that
'b' returns.

The first line is actually the result of undefined behaviour.
The correct way to do it is
printf("%p %p\n",(void *)&u.a,(void *)&u.b);

As for the second line whatever explanation might
exist is specific to your implementation. Your compiler
stores at &u.a the sequence of bytes which correspond
to float 200 and then you ask the compiler to interpret
this sequence of bytes as an integer. Actually depending
on the width of int and float on your platform it may be
that the compiler is not taking into account all bytes when
evaluating u.b or takes into account additional garbage
bytes. The result obviously depends on how your platform
stores these two types.
 
R

Richard Heathfield

(e-mail address removed) said:
union something
{
float a;
int b;
} u;

Ah, unions. Before you take something out, you must first put it in. If you
want to take out a float, you must put in a float. If you want to take out
an int, you must put in an int.

Here, you use static default initialiser rules for the file scope object, so
you are storing in the first-named object, a float. So the float has value
0.0f, and the value of the int is implementation-defined (and could be
anything, really).
int main()

Better, int main(void)
{
printf("%d %d\n",&u.a,&u.b);

The behaviour here, however, is undefined. You are calling a variadic
function without a valid prototype in scope. All bets are off. Also, you're
using %d %d when you should be using %f %d
u.a = 200;
printf("%f %d \n",u.a,u.b);
return 0;
}


The above code prints:

134518260 134518260
200.00000 1128792064

While the first line of output explains itself. I want to know what is
the relation between the value that we set for 'a', and the value that
'b' returns.

The value of b is implementation-defined if you store in a, and the
implementation is under no obligation to provide any kind of sensible rule.
 
G

Giorgio Silvestri

Richard Heathfield said:
(e-mail address removed) said:

[...]
{
printf("%d %d\n",&u.a,&u.b);

The behaviour here, however, is undefined. You are calling a variadic
function without a valid prototype in scope. All bets are off. Also, you're
using %d %d when you should be using %f %d

printf("%p %p\n",(void *)&u.a,(void *)&u.b);

[...]
 
R

Richard Heathfield

Giorgio Silvestri said:
Richard Heathfield said:
(e-mail address removed) said:

[...]
{
printf("%d %d\n",&u.a,&u.b);

The behaviour here, however, is undefined. You are calling a variadic
function without a valid prototype in scope. All bets are off. Also, you're
using %d %d when you should be using %f %d

printf("%p %p\n",(void *)&u.a,(void *)&u.b);

....adds nothing useful to the discussion that I can see. What point are you
trying to make?
 
G

Guest

Richard said:
Giorgio Silvestri said:
Richard Heathfield said:
(e-mail address removed) said:

[...]


{
printf("%d %d\n",&u.a,&u.b);

The behaviour here, however, is undefined. You are calling a variadic
function without a valid prototype in scope. All bets are off. Also, you're
using %d %d when you should be using %f %d

printf("%p %p\n",(void *)&u.a,(void *)&u.b);

...adds nothing useful to the discussion that I can see. What point are you
trying to make?

You suggested %f and %d to print pointer expressions. His point is that
the proper way to print pointer expressions is to use %p and convert to
void *.
 
R

Richard Heathfield

Harald van D?k said:
Richard said:
Giorgio Silvestri said:
"Richard Heathfield" <[email protected]> ha scritto nel messaggio
(e-mail address removed) said:


[...]


{
printf("%d %d\n",&u.a,&u.b);

The behaviour here, however, is undefined. You are calling a variadic
function without a valid prototype in scope. All bets are off. Also,
you're
using %d %d when you should be using %f %d

printf("%p %p\n",(void *)&u.a,(void *)&u.b);

...adds nothing useful to the discussion that I can see. What point are
you trying to make?

You suggested %f and %d to print pointer expressions.

Oh, good heavens, so I did. My apologies, Giorgio. I didn't even /see/ that
the OP was using &. I assumed (!) that he was trying to print the values of
the union members. Good spot.
 
G

Giorgio Silvestri

Richard Heathfield said:
Harald van D?k said:
Richard said:
Giorgio Silvestri said:


"Richard Heathfield" <[email protected]> ha scritto nel messaggio
(e-mail address removed) said:


[...]


{
printf("%d %d\n",&u.a,&u.b);

The behaviour here, however, is undefined. You are calling a variadic
function without a valid prototype in scope. All bets are off. Also,
you're
using %d %d when you should be using %f %d

printf("%p %p\n",(void *)&u.a,(void *)&u.b);

...adds nothing useful to the discussion that I can see. What point are
you trying to make?

You suggested %f and %d to print pointer expressions.

Oh, good heavens, so I did. My apologies, Giorgio. I didn't even /see/ that
the OP was using &. I assumed (!) that he was trying to print the values of
the union members. Good spot.

no problem of course! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top