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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:08:05+00:00 2026-05-24T04:08:05+00:00

The following struct X has 3 bytes of payload and 1 byte of padding:

  • 0

The following struct X has 3 bytes of payload and 1 byte of padding:

struct X
{
    short a;
    char b;
};

memory layout: aab.

The following struct Y has 4 bytes of payload and 2 byte of padding:

struct Y
{
    X x;
    char c;
};

memory layout: aab.c.

Is there any way to keep X nested inside of Y and have sizeof(X) == 4 && sizeof(Y) == 4?

memory layout: aabc

Ideally, I would want this kind of space optimization for all types X (think X as a template parameter).

  • 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-24T04:08:06+00:00Added an answer on May 24, 2026 at 4:08 am

    For hopefully obvious reasons the size of both X and Y can’t be four (if it were possible, X would have multiple definitions, the normal padded one and one where the padding is used by the char of Y). So the only way Y could be 4 is if X were changed to eliminate its padding. This can be done by compiler specific pragmas or directives. However, since that could result in X being accessed unaligned it would effectively restrict possible portability of your program (some architectures will even crash if misaligned accesses are made).

    Instead, can you explain more clearly the end result you’re hoping to achieve here?

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

Sidebar

Related Questions

I have a struct Primitive, which has the following definition: typedef struct Primitive {
I have the following struct: struct Datastore_T { Partition_Datastores_T cmtDatastores; // bytes 0 to
I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*
How can I convert the following struct to unsigned char* ? typedef struct {
Is there a way to clone an existing struct with different member alignment in
I'm wrapping a C api which has the following struct: typedef struct Value {
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have an array of structs. Each struct has the following two attributes: win
I have the following code: namespace FVProductions.Base { public struct Color { public byte
Has anyone got an emacs regexp handy to do the following generic replacement? (*ptr_to_struct).member_var

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.