0

I am wondering whether C packs bytes in the stack for optimal CPU retrieval even if they are outside of a struct. And if not, why do so specifically for a struct?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
madski
  • 377
  • 2
  • 10
  • *I am wondering weather* - I live in Scotland, its winter - probably cold, wet and windy. – Ed Heal Jan 23 '16 at 06:18
  • @EdHeal that makes sense! Thanks :) – madski Jan 23 '16 at 06:23
  • 1
    I wouldn't say that *"C packs bytes in the stack"*. Rather, *"the compiler properly aligns objects on the stack for optimal CPU retrieval"*. Note that aligning usually involves adding padding bytes, which is the opposite of packing bytes. – user3386109 Jan 23 '16 at 06:25
  • Why keep wondering when you could just [*Try It And See*](http://coliru.stacked-crooked.com/a/a929ebb80b54a6e1) – rici Jan 23 '16 at 06:26
  • Compilers normally align data types to their preferred alignment to speed access to them. – Tom Karzes Jan 23 '16 at 06:28
  • 1
    ... Although, of course, that experiment was just one possible implementation of C, which you might happen to find in the wild. – rici Jan 23 '16 at 06:28
  • @TomKarzes: what's the preferred alignment of a byte? :-) (And, I repeat, the experiment is easy to perform.) – rici Jan 23 '16 at 06:29
  • @rici It's usually 1, i.e. no padding required. Larger data types typically have more restrictive preferred alignments. – Tom Karzes Jan 23 '16 at 06:32
  • @TomKarzes: Precisely. So how does that fact assist in answering the question? – rici Jan 23 '16 at 06:36
  • 1
    @rici OP asked if compilers usually "pack bytes" on the stack. I assumed that "bytes" referred to bytes in any data type, not just `char`. And I answered the question, no, it usually aligns data to speed access. How is that not clear to you? – Tom Karzes Jan 23 '16 at 06:39
  • @tom: that interpretation of the question hadn't occurred to me, actually. You could well be right. In any case, every C type has an alignment which is independent of whether it is in a struct or free-standing. A compiler might overalign the stack, but it can't underalign it. – rici Jan 23 '16 at 06:44
  • @rici Right, but in gcc one can specify a "packed" attribute for a struct to eliminate padding. This can be useful to (a) minimize memory usage or (b) to match external data formats, such as data arriving from a serial bus for instance. – Tom Karzes Jan 23 '16 at 06:51
  • All variables are aligned. You just have to be a bit more aware of it for the members of a struct since the compiler aligns only the first member. – Hans Passant Jan 23 '16 at 08:06

1 Answers1

1

Structs are very widely used in C, and the compiler does various tricks for (1) alignment of objects for access speeds (2) for specific architecture mappings (ex in ARM - Thumb) where a developer can write code to map to Peripheral registers. But sometimes, we need explicit control for transmission across different systems (like network protocols).

From the point of view of embedded systems (ARM), below is a specific recommendation - "peripheral locations should not be accessed using __packed structs (where unaligned members are allowed and there is no internal padding), or using C bitfields. This is because it is not possible to control the number and type of memory access that is being performed by the compiler. The result is code which is non-portable, has undesirable side-effects, and will not work as intended".

Also see Structure padding and packing

Community
  • 1
  • 1
Prabindh
  • 3,356
  • 2
  • 23
  • 25