Sigh.

Community Forums/General Help/Sigh.

sswift(Posted 2009) [#1]
What the hell is a Union?


typedef struct tagRAWINPUT {
RAWINPUTHEADER header;
union {
RAWMOUSE mouse;
RAWKEYBOARD keyboard;
RAWHID hid;
} data;
} RAWINPUT, *PRAWINPUT; *LPRAWINPUT;



I found these pages describing it, but they do a terrible job and I don't understand what they're saying:

http://en.wikipedia.org/wiki/Union_%28computer_science%29
http://msdn.microsoft.com/en-us/library/y9zewe0d%28VS.80%29.aspx

I mean it looks like a union and a struct are the same thing and the above is nesting a union called data in the struct, so there would be a field in the struct called data, and that field would have subfields called mouse, keyboard, and hid that one could access, but if that's the case I don't understand why the outer one is declared as a struct but the inner one is declared as a union.


big10p(Posted 2009) [#2]
http://tinyurl.com/mleuu9


sswift(Posted 2009) [#3]
So basically, a union allows me to reference a single chunk of memory with ten different variable names?

Who came up with that crazy idea? Why not just have a single field and call it "data"? I mean hell, you have to access the fields by first typing "data" ANYWAY the way they have that particular example set up.

I mean maybe if you didn't have to type the name of the union to access the members it might have some use somehow in object oriented stuff, but since you do...

Hm... I guess they can be different types though. If you just had a variable called Data you couldn't do that...


big10p(Posted 2009) [#4]
Hm... I guess they can be different types though.
Yes. That's the power of unions. In your example, the data member can hold any one of three different types.


sswift(Posted 2009) [#5]
I thought the power of unions was that nobody could ever be fired or have their wages reduced?


big10p(Posted 2009) [#6]
That too.


sswift(Posted 2009) [#7]
It's still silly though. You're saving, what, a few bytes? At the cost of less readable code, and having to type a bunch more every time you want to access one of those vairables? Not to mention opening the potential for difficult to find bugs if you accidentally overwrite one of the values somewhere you shouldn't.


sswift(Posted 2009) [#8]
So what's this do?

typedef struct tagRAWMOUSE { 
  USHORT    usFlags; 
  union {
         ULONG    ulButtons;
             struct {
                       USHORT usButtonFlags;
                       USHORT usButtonData;
                       };
  };
  ULONG ulRawButtons;
  Long  lLastX;
  Long  lLastY;
  ULONG ulExtraInformation;
} RAWMOUSE, *PRAWMOUSE, *LPRAWMOUSE;


I've got a union, which I understand, but in that there's a struct, and there's no name listed after the struct to access the members with.

Am I to understand that with this setup, you either have ulButtons, OR you have both usButtonFlags and usButtonData?


sswift(Posted 2009) [#9]
typedef struct tagRAWKEYBOARD {
    USHORT MakeCode; ' 2
    USHORT Flags; ' 2
    USHORT Reserved; ' 2
    USHORT VKey; ' 2
    UINT Message; ' 4
    ULONG ExtraInformation; ' 4
} RAWKEYBOARD, *PRAWKEYBOARD, *LPRAWKEYBOARD;


UINT = Unsigned, 4 bytes, 0 through 4294967295
ULONG = Unsigned, 4 bytes, 0 through 4294967295

WHY HAVE BOTH?
WHY HAVE BOTH IN THE SAME STRUCT RIGHT NEXT TO EACHOTHER?

God, it's like they're intentionally trying to make things more difficult.

[edit]

Amazing. In one of the related structures they also have DWORDs. Which are the same exact thing as the above two as well, again, just with a different name!

I know why these different type idenfiers might all have come into existence, but to use them all in the same codebase, in the same set of structures? Ugh. It's like Microsoft has no code consistency.


big10p(Posted 2009) [#10]
Am I to understand that with this setup, you either have ulButtons, OR you have both usButtonFlags and usButtonData?

I believe so, yes (my C is a bit rusty, TBH). It seems to allow you to access the button data wholesale via ulButtons, OR access the same data, but broken down into it's specific components, via usButtonFlags and usButtonData.

WHY HAVE BOTH?
WHY HAVE BOTH IN THE SAME STRUCT RIGHT NEXT TO EACHOTHER?
Because C source is intended to be cross platform. While UINT and ULONG are the same here, they may not be on other architecture.


sswift(Posted 2009) [#11]
I don't buy that excuse. These are functions in a DLL. No matter what language you use, the returned data will always be the same size. Also because they're part of the OS and you want to maintain forward compatability, you will never want them to change size if the code is ever recompiled.

I've never liked the idea of ints being different sizes on different architectures. That sort of setup is just asking for memory leaks and other nasty bugs. And for what? So the code runs as fast as possible on whatever architecture it's running on? I guess that's a positive. But it still doesn't make any sense to use a variable type like that in a DLL whose interface should never change.


big10p(Posted 2009) [#12]
In this instance, I think the coder is just being anal. He/she is using a form of Hungarian Notation, after all. :)


Philip7(Posted 2009) [#13]
Google 'anal hungarian' and it wil all be explained.


big10p(Posted 2009) [#14]
If you do, you're unlikely to get any links to anything remotely programming related.


Gabriel(Posted 2009) [#15]
Google 'anal hungarian' and it wil all be explained.

You, Sir, have a very evil (and very Dutch) sense of humour. I like it!


Yahfree(Posted 2009) [#16]
This is why I don't like C++/C, it's waay over complicated sometimes. However, in some cases, it's a necessary evil. I avoid it whenever possible though :|


simonh(Posted 2009) [#17]
sswift, can you make sure these topics get posted in General Help please (and with a descriptive title). Moving...


N(Posted 2009) [#18]
sswift, you really ought to just learn C and/or C++ (the two are not the same, so learning C++ does not mean you learned C, and vice versa). How you survived this long without learning it is beyond me, but if you're using BMax and something outside the standard modules, it helps to know one or the other (typically C is more useful, from what I've seen).

This is why I don't like C++/C, it's waay over complicated sometimes.
This isn't complex. A union just gives you a much easier way to access different kinds of data in the same region.


simonh(Posted 2009) [#19]
Probably more convoluted than complicated. Half of the stuff I wouldn't worry about - like unions. A relic from the days when memory was scarce.


sswift(Posted 2009) [#20]
Nillium:

I knew C at one point. My first job was as a C programmer for a company that made game controllers.

I'd been programming my whole life, but within three months of switching to C I quit programming, and my job. I would not pick up a compiler again for three years, and then, only for Visual Basic, then Dark Basic, then the different flavors of Blitz.

Trying to figure out how these Windows calls works has only served to cement my resolve against getting back into C programming. Having to read through twenty pages of documentation just to get the X and Y position of the mouse isn't my idea of fun. I had to read almost as much to do that as I did to research complex graphics algorithms for stitching images together and calculating 3D shape from 2D shading!

C can go jump in a lake. :-)