C/C++ Struct Layout

Discussions center on memory layout, padding, alignment requirements, strict aliasing, and undefined behavior in C/C++ structs, including compiler optimizations, portability issues, and workarounds like packed structs or memcpy.

➡️ Stable 0.5x Programming Languages
3,920
Comments
20
Years Active
5
Top Authors
#744
Topic ID

Activity Over Time

2007
1
2008
5
2009
10
2010
41
2011
63
2012
82
2013
87
2014
182
2015
190
2016
326
2017
211
2018
367
2019
193
2020
391
2021
260
2022
428
2023
392
2024
277
2025
391
2026
23

Keywords

MS e.g CPU IMHO ARM C99 UB FWIW BTW s.a struct pointer char pointers compiler aliasing alignment padding memory undefined

Sample Comments

cesarb Oct 26, 2018 View on HN

It's undefined even in single threaded code. The compiler can assume it's aligned and make optimizations which depend on it.It sounds like what you were doing is marking a struct as "packed", and accessing the fields directly (instead of through a pointer to the field). That should be fine.

bjourne Jul 2, 2023 View on HN

That is of course not Kosher at all. Compilers are generally free to layout and pad structs in whichever way they prefer, meaning that two compilers for the same platform might very well use incompatible layouts. It can lead to major problems if structs saved by say a 64-bit version of the program is to be loaded by the 32-bit version.

InfiniteRand Sep 6, 2020 View on HN

Anonymous struts and unions allow fine grained control of memory layout in embedded code. (I am not 100% sure this is not UB but I have seen it in a lot of embedded code)Not sure if this is impossible in C++, I think some of the alignment functionality added in the past several versions might cover this, but it is not as simple.IMHO, Not a big reason since every compiler offers some non-standard keywords to cover this use case, but a reason nonetheless

ethelward Apr 22, 2020 View on HN

Except for _e.g._ memory alignment, or types of a given width (until C99).

api Dec 22, 2018 View on HN

In C/C++ you can easily do stuff that violates alignment constraints unless you totally avoid casts or packed structs.

cdavid Aug 5, 2016 View on HN

The compiler can't optimize this in general as this affects the ABI when the struct itself is exposed (that's one of the reason why it is useful to avoid exposing struct themselves but just pointer when you can).

dschatz Aug 18, 2012 View on HN

Nice introduction. I think it is worth pointing out that much of what you discuss is implementation dependent, the c standard doesn't require an implementation to lay out data in memory in any particular way. Instead it requires that access semantics behave in a particular way. These semantics, in turn, align with easy, low level implementations.

nknight May 4, 2012 View on HN

It's technically undefined behavior, but in practice it will work perfectly well on almost all C implementations, and in the infinitesimal number of cases where this structure is actually useful, portability isn't a major concern.

vnorilo Jan 8, 2019 View on HN

When you need this, use aligned storage: https://en.cppreference.com/w/cpp/types/aligned_storage

quelsolaar Apr 14, 2020 View on HN

Sure, it wont be portable between any architectures, but a lot of times you know you will be on a little endian platform where types are aligned to their sizeofs. That covers a lot of ground and the performance gains you get from optimizing with this in mind is significant. There is value in C being able to be portable, but there is also a huge value in being able to write non-portable code that takes advantage of what you know about the platform. C needs to acknowledge that that is a legitimate