Unions in c++

This is a discussion on Unions in c++ within the C++ Programming forums, part of the General Programming Boards category; I have just read a section in my c++ tutorial about unions . From what i have picked up a ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Unions in c++

    I have just read a section in my c++ tutorial about unions .
    From what i have picked up a union is a method of saving
    memory storing for example 2 short ints in a long int .

    Is that about right?

    Are unions becoming less used as memory becomes less of a
    problem?

    By using unions are you increasing efficiency in your programs?

    Thanks.

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    It is used in embedded systems. It can be used to reduce flags to the bit level instead of the byte level, but on the desktop, it probably isn't used too much. A C++er is more likely to use polymorphism and inheritance than to use structures and unions.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Its basically a nifty method of addressing a piece of memory in more than 1 way.....



    Code:
    #include <iostream>
    using namespace std;
    
    union MY_UNION{
    	unsigned short MyUShort;
    	long MyLong;
    	short MyShort;
    }MyUnion;
    
    int main(){
    
    	MyUnion.MyLong = 562789;
    
    
    	cout << dec << "MyLong = \t" << MyUnion.MyLong;
    	cout << hex << "\thex - \t0x0" << MyUnion.MyLong << endl;
    
    	cout << dec << "MyShort = \t" << MyUnion.MyShort;
    	cout << hex << "\thex - \t0x0" << MyUnion.MyShort << endl;
    		
    	cout << dec << "MyUShort = \t" << MyUnion.MyUShort;
    	cout << hex<< "\thex - \t0x0" << MyUnion.MyUShort << endl;
    
    	return 0;
    }
    Handy occasionally.......

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Guys,

    Are unions the kind of things i can worry about way down the
    track when i am comfortable with more important stuff like
    inheritance , virtual functions and operator overloading ?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by The Gweech
    Guys,

    Are unions the kind of things i can worry about way down the
    track when i am comfortable with more important stuff like
    inheritance , virtual functions and operator overloading ?
    No they arent that big an issue......

    Just remeber that they are used like structs but all the members point to the same point in memory........You can address it as a long, a char, an int whatever....but the same piece of memory is used

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Thanks Fordy thats the answer i was hoping for!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unions of the same size and pointers
    By holychicken in forum C Programming
    Replies: 9
    Last Post: 10-06-2008, 03:29 PM
  2. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 01:57 PM
  3. Structures - Unions
    By AProg in forum C Programming
    Replies: 16
    Last Post: 05-27-2003, 12:43 AM
  4. unions problems!
    By nextus in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2003, 03:04 AM
  5. Create Dynamic Array of Unions
    By Duncan Booth in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2002, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21