unions problems!

This is a discussion on unions problems! within the C++ Programming forums, part of the General Programming Boards category; i just read my book and it says union members share the same memory. like a long and a double ...

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    Question unions problems!

    i just read my book and it says union members share the same memory. like a long and a double will both occupy the same block of 8 bytes (the biggest member of the union). why would anyone want to do that? please explain in detail about unions.....and give some example because all my book gave me was:

    Code:
    union shareLD
    {
       double dval;
       double lval;
    };
    
    shareLD myUnion;
    myUnion.dval = 5.4;
    myUnion.lval = 5; // dval has been overwritten
    
    // or unions can be written like this
    
    union shareDLF
    {
       double dval;
       long lval;
       float fval;
    } uinst = {1.5};
    and it says unions can be use in classes...why wouldnt anyone
    want to use unions when they could use "new" and "delete"?
    nextus, the samurai warrior

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    also does that mean two or more members of the union cannot
    have values at the same time?
    nextus, the samurai warrior

  3. #3
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    Unions were used long ago when systems didn't quite have the memory to spare. They're really not used too much anymore. And since they all share the same memory, all the members will have the same value.

      __               &n bsp;      ___ & nbsp;       &nb sp;       &nbsp ;    
     /\ \  __    &nbs p;           /\_ \      &nbsp ;        & nbsp;     
     \_\ \/\_\  ____     _ __\//\ \    __  __&n bsp;    __   
     /'_` \/\ \/\_ ,`\  / __`\\ \ \  /\ \/\ \  /'__`\ 
    /\ \_\ \ \ \/_/  /_/\ \_\ \\_\ \_\ \ \_/ |/\  __/ 
    \ \___,_\ \_\/\____\ \____//\____\\ \___/ \ \____\
     \/__,_ /\/_/\/____/\/___/ \/____/ \/__/   \/____/
            &n bsp; I have a BAD figlet& nbsp;addiction.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >also does that mean two or more members of the union cannot
    >have values at the same time?

    Members of a union share the same memory. So if member A has a certain value, then member B, if the same type of A, also has that value.

    >Unions were used long ago when systems didn't quite have the
    >memory to spare. They're really not used too much anymore.

    Unions are still used a lot, not only to save memory, but also for uniform interfaces and other applications. A usefull application is for example making a union with a char and a bitfield in it, so that you can easily switch between using a char or access the individual bits.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  3. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  4. Unions in c++
    By The Gweech in forum C++ Programming
    Replies: 5
    Last Post: 08-06-2002, 08:14 AM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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