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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:03:11+00:00 2026-05-27T00:03:11+00:00

If I have a standard layout type like this: struct sl_t { int a;

  • 0

If I have a standard layout type like this:

struct sl_t
{
  int a;
};

And a union like this:

union un_t
{
  int b;
  double q;
};

Can I cast and use the union as the struct type? That is, may I assume that the union itself is a standard-layout type and the data is aligned at the start of the memory?

un_t obj;
sl_t * s = reinterpret_cast<sl_t*>(&obj);
s->a = 15;
assert( obj.b == 15 );

Or must I take the address of the variable in the union &obj.b?

Note that I’m already aware that if I store the struct inside the union the C++11 standard guarantees I may access both sl_t::a and un_t::b, referring to 9.5-1.

  • 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-05-27T00:03:12+00:00Added an answer on May 27, 2026 at 12:03 am

    Seems alignment is your issue, look at pragma pack. The struct/union names just references the allocated memory-block, if st_t.a is displaced in the struct by adding more members, your cast will fail, but if it remains the first member it will work since all members of the union points to the same address as the union itself.

    See section 9.2.17 – 21 of the C++-standard:
    “A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa.”

    See also section 9.5 Unions:
    “1. In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time. [ Note: One special guarantee is made in order to simplify the use of unions: If a standard-layout union contains several standard-layout structs that share a common initial sequence (9.2), and if an object of this standard-layout union type contains one of the standard-layout structs, it is permitted to inspect the common initial sequence of any of standard-layout struct members; see 9.2. — end note ] The size of a union is sufficient to contain the largest of its non-static data members. Each non-static data member is allocated as if it were the sole member of a struct.”

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

Sidebar

Related Questions

I have a standard python package layout like this: setup.py - using setuptools README
I have a pretty standard css layout where I use a container div that
I have a bunch of ASP.NET web pages (that have a standard layout) that
I am using Tiles within my web-application. I have a standard-layout (standard.jsp) within the
I have a subversion repository with the standard layout, i.e. trunk/ and branches/ (and
Currently we have a project with a standard subversion repository layout of: ./trunk ./branches
I've heard they have standard GUI guideline, but I can't seem to find a
I have a standard .NET windows service written in C#. Can it install itself
Based on some standard web searching, I have narrowed down my problem to this:
I have a standard project layout for a java project: project / src /

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.