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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:48:05+00:00 2026-06-10T20:48:05+00:00

I have a structure where the members have certain alignment requirements while no such

  • 0

I have a structure where the members have certain alignment requirements while no such requirement exist for the structure itself.

I’m using gcc so using __attribute__((aligned(n))) will do the trick, unless (as far as I know) an instance of the struct is allocated on the heap.

How do I keep the alignment for heap allocated instances? posix_memalign(3) will align the structure itself, but not the structure members, so I can’t see how to make it work with that function.

The source is here: https://github.com/colding/disruptorC/blob/master/src/disruptor.h#L92

  • 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-10T20:48:07+00:00Added an answer on June 10, 2026 at 8:48 pm

    No matter where a struct is—stack or heap—the layout of the struct must be the same. The compiler ensures that the sizeof() and the layout of elements within the struct match the alignment requirements (via padding). It also gives the struct itself a required alignment so that its members end up on the right boundary (this value is the largest alignment of any of its members).

    So just use posix_memalign and you’ll be fine:

    MyStruct* ptr;
    posix_memalign(&ptr, alignof(MyStruct), sizeof(MyStruct));
    

    For example, if you have this definition:

    struct MyStruct {
        char c;
        double d;
    }
    

    It’s compiler-dependent, of course, but the most likely behavior is that the compiler lays out the following:

    • 1-byte char
    • 7 bytes of padding
    • 8-byte double

    and gives the whole thing an alignment of 8 bytes. Then, if the struct itself is aligned properly (on an 8-byte boundary), the double that’s 8 bytes offset into it will also be properly aligned.

    (alignof is different in different compilers/standards: __alignof__ in gcc, __alignof in MSVC, and alignof in C11/C++11.)

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

Sidebar

Related Questions

I have a structure defined in IDL. This structure has following members: { BSTR
I have this ordered List structure that has a structure that has two members,
I have a large C structure (about 40 members, int and char[]) which I
Container requirements have changed from C++03 to C++11. While C++03 had blanket requirements (e.g.
I have a structure with no members (for the moment) and I would like
I have a structure which I create a custom constructor to initialize the members
How to print a particular member of a structure using pointer arithmetic? I have
I am puzzled by the errors i get for referencing structure members. I have
i have an array structure like [members] => Members List | 26 [member.php?id=3] =>
I have an array that is a member of a structure: $self->{myArray} = [value1,

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.