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 8227635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:11:26+00:00 2026-06-07T16:11:26+00:00

I know that for the code below, Illegal below is undefined (while some compilers

  • 0

I know that for the code below, “Illegal” below is undefined (while some compilers allow it), because union member “a” is active, and then we read from union member “b”.
The question is, does the code in “AmILegal” fix it, or am I doing something scary and even more obscure? Can I use memcpy to achieve the same effect or is there another undefined behaviour I am invoking there?

EDIT: Maybe the example is not clear enough. All I want to do is activate the other member.
So I am changing float to int. Although it seems dumb, it is closer to the real case. Read BELOW the code.

(Is it for some reason disallowed to copy one union member into another?)

struct Foo
{
    union Bar
    {
        int a[4];
        int b[4];
    };

    void this_is_Illegal()
    {
         a[0]=1;
         a[1]=2;
         a[2]=3;
         a[3]=4;
         std::cout<<b[0]<<b[1]<<b[2]<<b[3];
    }

    void but_is_this_Legal?()
    {
         a[0]=1;
         a[1]=2;
         a[2]=3;
         a[3]=4;

         b[0]=a[0];
         b[1]=a[1];
         b[2]=a[2];
         b[3]=a[3];
         std::cout<<b[0]<<b[1]<<b[2]<<b[3];
    }


    void this_looks_scary_but_is_it?()
    {
         a[0]=1;
         a[1]=2;
         a[2]=3;
         a[3]=4;
         //forget portability for this q, assume sizeof(int)==sizeof(float)
         //maybe memmove works here as well?
         memcpy(b, a, sizeof(int)*4)

         std::cout<<b[0]<<b[1]<<b[2]<<b[3];
    }

};

If all of the above does not sound very useful, think that a is in truth an _m128 unioned with a float[4]. The bit representation is exact and correct, always.
At one point in time, you WILL need to actually use it, and you NEED to have it in main memory as an array of floats.
The “copy instruction” is in truth an _mm_store_ps from the _m128 union member to the float[4] member. Hence the question about the memset – maybe it is the more exact example to what I need…

  • 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-07T16:11:27+00:00Added an answer on June 7, 2026 at 4:11 pm

    The second function is perfectly legal – but doesn’t do the same thing, since it will perform an int to float conversion rather than leaving the bits unchanged.

    To be honest I would just stick with the first one – the behaviour is technically undefined, but I suspect it just does the right thing for you.

    The third one switches one form of undefined behaviour for another (once you’ve written arbitrary bytes into a float, anything could happen). But if you know the bytes really represent a valid floating point value, it’s fine.

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

Sidebar

Related Questions

UPD. Hello, I know how code below is working. I know that cross, and
I've been developing with JS for a while, and while I know that code
I know that the below code is a partial specialization of a class: template
Regarding the code below, how does the interpreter know that event has attribute .keyCode.
The code below explains best what I'm trying to accomplish. I know that I
Ok, before you review the code below, I know that it is AWFUL. It's
I know that c++ code should be compiled and linked by g++, not gcc.
I know that GUI code in Java Swing must be put inside SwingUtilities.invokeAndWait or
I have code that uses jquery.slideup and jquery.slidedown How can i know that div
I know that the following code should show and hide a tiny circular progress

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.