last day (14 days later) » 

3:15 PM
5
A: Storage of Union In Memory

Baum mit AugenReading a member of a union that is not the one you last wrote to is undefined behavior. This means that your code could do anything and arguing about its behavior is not meaningful. To perform conversion between types, use the appropriate cast, not a union. To answer your questions after the e...

 
......... What?
 
@KunalGupta After you write to eps.i you are only allowed to read eps.i until you write to another member. Then you can read only that one.
 
So, only one of the members to be I/O'd at a time?
I know I could use conversion using casts, but what is exactly causing such behavior?
 
@KunalGupta The rule is simply: You can only read from the member you last assigned to. You cannot use union to convert types like you did, this is what casts are for.
 
Could you read the question once again? I have edited it to neatly ask my questions (3 of them).
Question edited for clarification.
Two members of the same type will probably use the exact same memory, but you cannot rely on that, They might or might not live in the exact same memory - When? And when do they not? (Do not involve structure or any similar techinique. I mean on while adding variables simply as mentioned in the edit.)
 
3:15 PM
@KunalGupta "When?" When the compiler feels like it. "When not?" When it does not. You cannot know how the union will look internally. You just need to accept the one basic rule I told you.
 
Union does mean that the share memory, and For e.g. if I have more than one variable like int i,j,k; do they all reside in the same location and while this happens, do they act like references to each other?
 
No. As I said, you can access objects through a reference.
You cannot access a union member that is not the one you last wrote to.
 
According to guidelines, right?
 
According to the C++ standard.
If you do anyway, it may seem to work until Jupiter and Mars line up and then format your HDD.
 
I like the joke there.
But I am just trying to learn.
 
3:18 PM
You can never rely on undefined behavior because it is not defined what it does.
 
i, j point to the same byte(s)?
But it just depends upon the environment how i and j are read?
 
Maybe. You cannot know.
 
So, what does union actually do? Doesn't it share memory with its members?
Or
 
If the compiler for same strange reason wants to, it can save i like a normal int and j backwards and the fix this every time you read j.
 
So, environment dependency, right?
 
3:21 PM
In that case i and j would live in the exact same bytes, but reading one trhough the other would still result in garbage.
 
Thanks, I get it now.
Compiler is pretty badass.
 
Yeah, it most likely will not do such things.
But UB stays UB and we programmers need to stay away from it.
 
Yeah, right.
Could you post this chat in your answer for future users to see?
 
There is a link to it in the comments. For most users, my answer should be sufficient I think.
 
Thanks.
 
3:25 PM
No problem.
 

last day (14 days later) »