C++ Unions to understand - question
Hi!
I began to learn C++. It goes quite well. I have understood many important part of it and some concepts however there is a little bit more complicated thing (unions) that I cannot "digest". Correct if I am wrong: with a union you can access the same memory allocation with different data types. So it is like a pointer. You can point to a certain memory location and read the data there as byte or character or whatever. Is that correct so far? So if I read in a character from console with cin and store it in a var like char c, that I should be able to access it as an byte if I point of reffer to it. Now the question would be, how can I use the unions. I read some examples but I couldn't find any practical examples, therefore I cannot use it, however it is a handy feature. I'd be glad if someone gave ma a good example (simple one pls that is not implemented into a struct or similar). Thanx |
Quote:
A useful example of how to use one is when you want to store some type of data into a variable, say the current temperature, but want the user to be able to decide what kind of precision to use. Imagine the user selects to just store the temperature as an integer; you can set a flag in your program to say it's stored as an int, and then store it into the int field of the union. Then later on say the user changes their mind and wants to store it as a float; you set the flag to indicate storage as a float, and assign to the float value of the union. Note that you can still try to get the integer value of the union, but it won't be correct -- it will be the data of the float interpreted as an int, which is usually not what you want. Unions in C and C++ have a storage size equal to the largest member of the union. Other languages (like Ada and Standard ML) have, in contrast to C and C++, safe unions. This means that the variable that represents the union carries it's type information with it at runtime, and doesn't allow itself to be coerced into a type it's not holding. This feature is often used along with type checking to take different actions depending on the type of the variable. A typical example is a binary tree. Each node on the tree is either (a) a leaf holding some value, or (b) a value with a left child node and a right child node. Both (a) and (b) are different types, the general node is a union of those two types. A search function can take a general node, and examine it's type. If it's a leaf, check the value and return whether it's value is equal to the search key. If it's an interior node, check the value and compare it to the search key. If the values are the same, return true. If the values are not the same, recurse and pass either the left or right child node as the general node to the search function. But unfortunately cool features like these can only be used in languages supporting safe unions. In C and C++, my advice is to stay away from them whenever possible. |
Quote:
Code:
template<typename T> |
Quote:
all examples will probably work correctly only if structures are 1-byte aligned. Quote:
Code:
#include <iostream> Quote:
Code:
#include <linux/types.h> Code:
#include <linux/types.h> Code:
struct Something{ IN practice you will need unions only if you are dealing with some binary data, that is located in memory, and is created by some external program that can't be (for some reason) currently modified. If you have a raw block of data you can use unions (in certain cases) to provice several ways to access it's content. Another example/way to use unions: imagine you are developing a script language where variable can be anything (with certain restrictions, of course) 4bytes long - pointer(32bit system), float, dword. If you'll have to implement a structure/class for that variable (and your program will need a several hundred thounsands of those variables ;)) you'll probably use unions to save some memory. Code:
enum VariableType = {Dword, Float, Pointer, None}; |
Ahm... Wow.. I didn't expect that you would actually take the time and explain me that in such great details. I am very impressed. Thanx guys...
I have a question in connection with these: Quote:
Thanx one more time |
Quote:
__u32 is an unsigned integer 32bit long. Those are non-standart types (declared in <linux/types.h>), they should exist in another distributions, but I can't guarantee that. In fact I've used them only because I've just recently migrated from windows and still do not know if there is some other "DWORD" type that is guaranteed to be 4bytes long on any platform, and if there is some "WORD" type that is guaranteed to be 2 bytes long on any platform. I've found __u16 and __u32 in linux include directory while browsing svgalib sources. They looked like a good "DWORD" and "WORD" replacement, so I just took them. I don't know for sure if those types are standart or not, and if they are portable or not. |
Have a look at stdint.h, it provides types which are more portable, int*_t and uint*_t.
|
If once you have compiled the app with static libs than no matter what's on the other comp, it should run, is that right? Bigger program, maybe slower but more compatible.
|
Quote:
Code:
#include <iostream> ta0kira |
Ahm... OK... I am quite into programming but this is too high for me. :confused:
Thanx though.. |
Quote:
|
Sorry, I was just demonstrating to taylor_venable that unions are quite useful despite having loads of memory. Basically what the union in my other post does is this:
Code:
union: ta0kira |
Quote:
Quote:
Quote:
http://www.xml.com/ldd/chapter/book/ch10.html Quote:
|
Yeah, whatever...
Thanx for correcting me. It is always nice to know the accurate things. |
Quote:
|
All times are GMT -5. The time now is 11:38 AM. |