Good use for Unions

Discussion in 'C++' started by Michael, Jan 24, 2005.

  1. Michael

    Michael Guest

    Hi guys I've been programming C++ for quite some time, but I have never used
    unions. I am trying to find a good exmaple of when they might be used. can
    anyone provide one??
    Regards

    Michael.
     
    Michael, Jan 24, 2005
    #1
    1. Advertisements

  2. Look up "VARIANT" data type in Microsoft's OLE and COM documents.

    V
     
    Victor Bazarov, Jan 24, 2005
    #2
    1. Advertisements

  3. Michael

    Efrat Regev Guest

    I can't think of a very common case. However, in some recursive data
    structures (e.g., trees), you might want each conceptual node to be either a
    "real" node or a "nill" node. There are many different ways of implementing
    such a data structure. Suppose you're *really* concerned about indirection
    (i.e., you wish to avoid it). Then you might create a vector whose each
    element an object of
    struct node
    {
    bool m_real_node;

    union m_true_node
    {
    real_node m_real_node;

    nil_node m_nil_node;
    }
    };

    So that's a case where you might use a union. As far as I understand, the
    above (quite ugly) example should be used in very extreme circumstances (if
    you're convinced this will actually improve performance), and you should
    encapsulate the hell out of it into a container employing such nodes
    internally.

    By the way, I vaguely recall the above code working ~3 times as fast as a
    version I wrote without unions (based on pointers, and using NULL to signify
    a "nil" node) (of course, since I wrote them both, who knows what I might
    have fudged, also, it's been years, and I can't recall the exact settings).
     
    Efrat Regev, Jan 24, 2005
    #3
  4. Michael

    shez Guest

    Force alignment of a buffer:

    union {
    char buffer[1000];
    int dummy;
    };

    This forces the compiler to start 'buffer' at an integer boundary.
    -shez-
     
    shez, Jan 25, 2005
    #4
  5. If you're developing a specialized container class which houses builtin
    data types as to provide your other interfaces with a common object to
    pass around as data. For example;

    class Data
    {
    union
    {
    char c;
    int i;
    float f;
    };

    type getType();
    operator char();
    operator int();
    operator float();
    };

    et cetera.
     
    Hayden A. James, Jan 25, 2005
    #5
  6. Michael

    Aslan Kral Guest

    I use it for something like this for example.

    union unionPw
    {
    int isPasswordSet;
    char szPassword[16];
    };

    isPasswordSet = 0; // initialize
    ....
    strcpy(szPassword, "password"); // isPasswordSet is set to a non-zero value
     
    Aslan Kral, Jan 26, 2005
    #6
  7. What happens if the first sizeof(int) bytes of "password" happens to be
    a trap representation for int?
     
    Clark S. Cox III, Jan 26, 2005
    #7
  8. Michael

    Aslan Kral Guest

    On Intel for sizeof(int)=4
    before strcpy: isPasswordSet = 0
    after strcpy: isPasswordSet = 0x73736171 ("pass")

    So you can do something like this after strcpy:

    if ( isPasswordSet )
    {
    ....
    }
    else
    {
    ....
    }

    What do you mean by "trap representation for int"?
     
    Aslan Kral, Jan 26, 2005
    #8
  9. Michael

    Andre Kostur Guest

    No, you can't. It is undefined behaviour to read from any member of the
    union that wasn't the last one that was set. Since the last thing you
    set was szPassword, you may not read from isPasswordSet.
    There may exist certain bit patterns for an int that represents an
    invalid value. By attempting to use it, the CPU (or something else) may
    cause some sort of trap causing your application to crash.
     
    Andre Kostur, Jan 26, 2005
    #9
  10. Michael

    Aslan Kral Guest

    How do you think it will be decoded as an opcode by the CPU?
    It is just a value in the data segment.
     
    Aslan Kral, Jan 26, 2005
    #10
  11. Michael

    Andre Kostur Guest

    Doesn't need to be an executable opcode. It is theoretically possible that
    on the UberCPU 9000 platform, in order to perform an operation on an int,
    it needs to load it into a register. That register may have restrictions
    on what constitutes a legal int. When you then load an invalid int
    representation into that register, BOOM.
     
    Andre Kostur, Jan 26, 2005
    #11
  12. Michael

    Aslan Kral Guest

    Hmm. I didn't know that. The value in my code was defined for a zero test.
    Of course things like divide by zero exception or buffer overflow may happen
    if you
    are not careful. Other than those things I don't see any problem for Intel
    platform.
    That is not the subject. I was only giving a simple example. I don't it
    creates such
    problems.
     
    Aslan Kral, Jan 26, 2005
    #12
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.