Philippe Aubertin
Philippe Aubertin

Reputation: 1063

GCC end-aligned structure

I am working on a memory allocator. Each allocated buffer starts on an 8-byte boundary and each is preceded by a header for managing the allocation (the header is immediately before the 8-byte-aligned buffer).

The header looks like:

struct header {
    uint32_t     hword32;
    void        *hpointer;
};

Assuming that all members of this structure are packed and that a pointer is 64-bit, this structure has a length of 12 bytes and the alignment of each member is correct (because the end of the structure is 8-byte-aligned).

Accordingly, I would like to tell my compiler two things:

  1. Do not add padding between the two members of the structure; and
  2. Don’t worry, about the alignment of the members, they are aligned.

How can I do this with gcc?

It is tempting to use __attribute__((packed)). However, while this does the trick for 1., my understanding is that it does not cover 2. (i.e. it causes the compiler to insert code to handle unaligned accesses, which is not needed here).

Upvotes: 1

Views: 61

Answers (0)

Related Questions