This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to align but with offset

Say I need to allocate struct A but b needs to be 4 aligned.

struct A
{
    char a;
    char b[42];
};

I guess I can manually pad a pointer returned by malloc(). Any cleaner way? Like gcc's __attribute__((aligned(4))) but that aligns the whole structure. And I can't change it's layout.

  • ... but that aligns the whole structure

    Member alignment effectively builds on top of the structure base alignment, so you need the structure alignment to be alignas(4) in any case.

    And I can't change it's layout.

    If you can't add padding between a and b, so you need "a" on a weird base alignment you're definitely "off the grid" in terms of standard features the language can provide. If you must do this, then I think you're going to have to pad malloc return pointers by hand.