Does `__attribute((packed))` affect the alignment of other data structures?

90 Views Asked by At

I am trying to understand why alignment matters when dealing with data structures and why would it affect memory access performance. I stumbled upon a C syntax, __attribute__((packed)); which what I understand, signals the compiler to not pad extra bits for alignment.

Say:

struct sampleStruct{
    uint8_t foo;
    uint8_t anotherFoo;
};

foo and anotherFoo equals to 16-bits but add an extra 16-bits to align with the natural word size of 32-bits processors.

Okay let's add another data structure:

struct anotherStruct{
    uint8_t fooZero;
    uint8_t fooOne;
}__attribute__((packed));

struct sampleStruct{
    uint8_t foo;
    uint8_t anotherFoo;
};

My question is, when initializing a packed data structure does it affect how other data structures are aligned in memory?

  • Maybe the compiler re-orders how these data structure are created? (sampleStruct then anotherStruct)
  • How about when there are two programs running on a system with the first program having an unaligned struct while the second program has an aligned struct? Would it leave a "gap" in memory or would the other struct be unaligned?

Sorry for the dumb question. This has been boggling my mind for quite some time. I want to know how memory alignment works and how it affects the processor in accessing memory.

2

There are 2 best solutions below

2
dbush On

Generally speaking, a variable that takes up n bytes can be read / written more effectively from memory if it resides at an address divisible by n. On some processors (notably, not x86) attempting to read / write in an unaligned manner can cause a hardware trap.

So while packing a structure can reduce its size, it typically means slower memory access (if access can happen at all).

Packing a structure doesn't affect other structures or other types in general. Each different type, whether it be a "base" type or a derived type, has its own alignment, so a variable of a given type is placed based on that type's alignment and where there may be available space. The alignment of variables of some other type won't have a direct effect on that.

As for other processes, they are completely independent of each other so there's no reason that the internal data structures of one would have any affect on the internal data structures of another.

0
ikegami On

You don't seem to understand what alignment is or what packed does.

Alignment just means the object must be found/placed at an address that's divisible by some number. The alignment of one type has no bearing on the alignments of other types in this or other programs or processes, except when one type (array, union, structure) contains an object or another type.

Improper alignment can result in performance penalty (or worse).

Since your structure isn't affected by packed since it doesn't use any padding, so let's consider a different structure.

struct yetAnotherStruct {
    uint8_t  foo1;
    uint32_t foo2;
    uint8_t  foo3;
};

Without __attribute__((packed)), this might compile as:

  • The struct has a size of 12.
  • foo1 is a uint8_t found at offset 0.
  • foo2 is a uint32_t found at offset 4.
  • foo3 is a uint8_t found at offset 8.
  • Must be placed at an address divisible by 4.
+---+---+---+---+---+---+---+---+---+---+---+---+
| 1 | padding   | 2             | 3 | padding   |
+---+---+---+---+---+---+---+---+---+---+---+---+

With __attribute__((packed)), this compiles as:

  • The struct has a size of 6.
  • foo1 is a uint8_t found at offset 0.
  • foo2 is a uint32_t found at offset 1.
  • foo3 is a uint8_t found at offset 5.
  • Must be placed at an address divisible by 1.
+---+---+---+---+---+---+
| 1 | 2             | 3 |
+---+---+---+---+---+---+

Those numbers are all it changes. Code that uses structs of this type uses those numbers to generate the appropriate CPU instructions to access, modify or allocate the structure. But that's it.

It doesn't affect any other structures in this or other programs in this or other processes (unless those structures contains this structure).