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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:52:51+00:00 2026-06-09T18:52:51+00:00

I want to have named fields rather than indexed fields, but for some usage

  • 0

I want to have named fields rather than indexed fields, but for some usage I have to iterate on the fields. Dumb simplified example:

struct named_states {float speed; float position;};

#define NSTATES (sizeof(struct named_states)/sizeof(float))
union named_or_indexed_states {
   struct named_states named;
   float indexed[NSTATES];
}
...
union named_or_indexed_states states,derivatives;
states.named.speed = 0;
states.named.position = 0;
...
derivatives.named.speed = acceleration;
derivatives.named.position= states.named.speed;
...
/* This code is in a generic library (consider nstates=NSTATES) */
for(i=0;i<nstates;i++)
    states.indexed[i] += time_step*derivatives.indexed[i];

This avoid a copy from named struct to indexed array and vice-versa, and replace it with a generic solution and is thus easier to maintain (I have very few places to change when I augment the state vector).It also work well with various compiler I tested (several versions of gcc/g++ and MSVC).

But theorically, as I understand it, it does not strictly adhere to proper union usage since I wrote named field then read indexed field, and I’m not sure at all we can say that they share same struct fields…

Can you confirm that’s it’s theorically bad (non portable)?

Should I better use a cast, a memcpy() or something else?

Apart theory, from pragmatic POV is there any REAL portability issue (some incompatible compiler, exotic struct alignment, planned evolutions…)?

EDIT: your answers deserve a bit more clarification about my intentions that were:

  • to let programmer focus on domain specific equations and release them from maintenance of conversion functions (I don’t know how to write a generic one, apart cast or memcpy tricks which do not seem more robust)
  • to add a bit more coding security by using struct (fully controlled by compiler) vs arrays (decalaration and access subject to more programmer mistakes)
  • to avoid polluting namespace too much with enum or #define

I need to know

  • how portable/dangerous is my steering off the standard (maybe some compiler with aggressive inlining will use full register solution and avoid any memory exchange ruining the trick),
  • and if I missed a standard solution that address above concerns in part or whole.
  • 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-09T18:52:53+00:00Added an answer on June 9, 2026 at 6:52 pm

    There’s no requirement that the two fields in named_states line up the same way as the array elements. There’s a good chance that they do, but you’ve got a compiler dependency there.

    Here’s a simple implementation in C++ of what you’re trying to do:

    struct named_or_indexed_states {
        named_or_indexed_states() : speed(indexed[0], position(indexed[1]) { }
        float &speed;
        float &position;
        float indexed[2];
    };
    

    If the size increase because of the reference elements is too much, use accessors:

    struct named_or_indexed_states {
        float indexed[2];
        float& speed() { return indexed[0]; }
        float& position() { return indexed[1]; }
    };
    

    The compiler will have no problem inlining the accessors, so reading or writing speed() and position() will be just as fast as if they were member data. You still have to write those annoying parentheses, though.

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

Sidebar

Related Questions

Suppose I have a entity with a id and a name fields. I want
I have a folder named img and i want to use js to look
I have a range named STATE. I want to set data validation in range
I have a UIWebView named wView. I want to use webViewDidFinishLoad: in my class,
I have a form named reg.php and its action is reg.php , i want
Considering that I have a Schema named SBST I want to find all empty
I have a Class named Question and a related Class named Answers. I want
I have one control named thumbviewer inside repeater. I want to set its imageurl
I have a database table named Categories . I want to create a form
I have 2 apps that I want to make communicate via named pipes on

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.