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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:05:26+00:00 2026-06-09T17:05:26+00:00

This question is related to How to ensure a member is 4-byte aligned? Example:

  • 0

This question is related to How to ensure a member is 4-byte aligned?

Example:

struct Aligned
{
    char c;
    __attribute__((__aligned__(4))) int32_t member;
}

struct Test
{
    char    c;
    Aligned s;//is s.member 4 bytes aligned?
}


void f1()
{
    char    c1;
    Aligned s;//is s.member 4 bytes aligned?
    char    c2;
}

void f2()
{
    Aligned* s = new Aligned();//is s.member 4 bytes aligned?
}

Can you please explain if “member” is 4 bytes aligned in all cases, and if yes how this works?

Edit:
I forgot the case where Aligned is derived from other struct:

struct Aligned : public SomeVariableSizeStruct
{
    char c;
    __attribute__((__aligned__(4))) int32_t member;
}

Second Edit: My questions is: is always first member of a structure 4 bytes aligned? Because in all cases presented here the first variable address might not be 4 bytes aligned and the 3 bytes padding doesn’t guarantee that “member” is 4 bytes aligned

  • 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-09T17:05:27+00:00Added an answer on June 9, 2026 at 5:05 pm

    Yes, it works. The compiler is required to make it work.

    Every type has an alignment which the compiler has to respect. You’ve specified that one particular object should use 4-byte alignment, but every other object, a char, an int, a std::string and anything else also have their own alignment requirements.

    So the compiler is used to having to cope with this.

    To determine the alignment required for Aligned, the compiler basically goes through all its members (as well as its base class(es) to find the one that requires the strictest alignment. This then becomes the alignment required for Aligned as well.

    The most aligned member in this case is member, which requires 4-byte alignment. So Aligned requires 4-byte alignment.

    And yes, this is guaranteed whether you create an Aligned on the stack as a function local variable, or as another class member, or on the heap with new.

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

Sidebar

Related Questions

Please note this question related to performance only. Lets skip design guidelines, philosophy, compatibility,
This question is related to another question I wrote: Trouble using DOTNET from PHP.
This question is related to the one I asked here . I'm trying to
This question is related to another question, where I wanted to define a custom
This question is related to my previous post Image Processing Algorithm in Matlab in
This question is related to this SO post Rather than using a recursive CTE
This question is related to awk, I suppose. I have no experience with awk.
This question is related to another question of mine. Thanks to some help I
This question is related to database/RDBMS where i have little knowledge regarding performance and
This question is related somewhat to the one i asked HERE . Now, i

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.