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

  • Home
  • SEARCH
  • 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 7033157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:01:43+00:00 2026-05-28T01:01:43+00:00

Suppose I have a struct, be it union’d or otherwise: typedef struct { union

  • 0

Suppose I have a struct, be it union’d or otherwise:

    typedef struct {
        union {
            struct { float x, y, z; } xyz;
            struct { float r, g, b; } rgb;
            float xyz[3];
        } notAnonymous;
    } Vector3;

I’ve heard that some compilers automatically pad structs to enhance performance by creating word-aligned boundaries.

Presumably such synergy means the size of a struct cannot be guaranteed to be the sum of its component field sizes, and therefore there is a change of data corruption and/or overflow for array xyzs in the following:

inline Vector3 v3Make(float x, float y, float z) { Vector3 v = {x,y,z}; return v; }
float xyzs[6];
*(Vector3*)&xyzs[3] = v3Make(4.0f,5.0f,6.0f);
*(Vector3*)&xyzs[0] = v3Make(1.0f,2.0f,3.0f);

Correct?

  • 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-28T01:01:44+00:00Added an answer on May 28, 2026 at 1:01 am

    It’s true the compiler can lay our your structure with what ever kind of padding it wants. You can use #pragma pack or __attribute__((packed)) to avoid padding on most compilers. In practice, you have three 32-bit fields in there, so it’s probably not going to be a problem. You can check by using sizeof on your structure type or a variable of that type and seeing what comes out.

    What is a problem is that you’re trying to assign a Vector3 to a float variable in your last two lines. That’s not going to be allowed. You could hack in what you’re trying to do:

    *(Vector3 *)&xyzs[3] = v3Make(4.0f, 5.0f, 6.0f);
    

    But that’s pretty ugly looking, not to mention confusing. It would be a lot better to change xyzs to being an array of Vector3 rather than of just float.

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

Sidebar

Related Questions

Suppose I have a struct called foo_boolean that contains some boolean values: struct foo_boolean
Suppose I have a structure: typedef struct { float x; float y; float z;
Suppose I have the following struct and function returning a pointer: typedef struct {
Suppose I have the following: typedef struct { int itemSize; int count; void *list;
Suppose I have some data stored in a container of unique_ptr s: struct MyData
Quick question... Suppose I have a C# struct that contains a single reference member.
Suppose I have some simple struct like this: public struct WeightedInt { public int
Suppose I have a C++ struct that has both POD and non-POD member variables:
Suppose we have two structs: typedef struct Struct1 { short a_short; int id; }
Suppose I have: struct Magic { Magic(Foo* foo); Magic(Bar* bar); }; Is there a

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.