Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:15:10+00:00 2026-06-10T11:15:10+00:00

I have a multithreaded application that stores data as an array of instances of

  • 0

I have a multithreaded application that stores data as an array of instances of the following union

union unMember {
    float fData;
    unsigned int uiData;
};

The object that stores this array knows what type the data in the union is and so I dont have problems with UB when retrieving the correct type. However in other parts of the program, I need to test equality between 2 instances of these unions and in this part of the code the true internal data type is not known. The result of this is that I can’t test equality of the union using this kind of approach

  unMember un1;
  unMember un2;
  if (un1 == un2) {
     // do stuff
  }

as I get compiler errors. As such I am simply to compare the float part of the union

  if (un1.fData == un2.fData) {
     // compiles but is it valid?
  }

Now given that I have read about it being UB accessing any part of a union that was not the part that was last written to (that is cumbersomely written but I can think of no more articulate way to say this) I am wondering if the code above is a valid way to check equality of my union instances??

This has made me realise that internally I have no idea how unions really work. I had assumed that data was simply stored as a bit pattern and that you could interpret that in whatever way you like depending on the types listed in the union. If this is not the case, what is a safe/correct way to test equality of 2 instances of a union?

Finally, my application is written in C++ but I realise that unions are also part of C, so is there any difference in how they are treated by the 2 languages?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:15:11+00:00Added an answer on June 10, 2026 at 11:15 am

    In general, you need to prepend some kind of indicator of the current union type:

    struct myData
    {
        int dataType;
        union {
            ...
        } u;
    }
    

    Then:

    if (un1.dataType != un2.dataType)
        return (1 == 0);
    switch(un1.dataType)
    {
        case TYPE_1:
            return (un1.u.type1 == un2.u.type1);
        case TYPE_2:
            ...
    }
    

    Anyway, the syntax

    if (un1.fData == un2.fData) {
        // compiles but is it valid?
    }
    

    which does compile and is valid, might not work for two reasons. One is that, as you said, maybe un2 contains an integer and not a floating point. But in that case the equality test will normally fail anyway. The second is that both structures hold a floating point, and they represent the same number with a slight machine error. Then the test will tell you the numbers are different (bit by bit they are), while their “meaning” is the same.

    Floating points are usually compared like

    if (dabs(f1 - f2) < error)
    

    to avoid this pitfall.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a data structure in a multithreaded application that I want to
I have a multithreaded application that has many concurrent operations going on at once.
I have a multithreaded application that makes heavy use of OpenSSL in C. It
In a multithreaded application. I have a bunch of function that loop through a
I currently work on a multithreaded application where I receive data using the following
I have a multithreaded application written in c#. What i noticed is that implementing
I have a Delphi 6 application that is heavily multithreaded. I have a component
I have a Segmentation fault in my multithreaded application that is becoming a headache.
I have C# multithreaded application that has to interface with a hardware using SerialPort.
I have a multithreaded application that is using LinqToSql. One of the things I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.