RFR: 8274400: HotSpot Style Guide should permit use of alignof [v3]

Kim Barrett kbarrett at openjdk.org
Sat Jan 28 13:16:16 UTC 2023


On Sat, 28 Jan 2023 00:03:41 GMT, Xin Liu <xliu at openjdk.org> wrote:

> > In order to access through an object, the object must be correctly aligned for its type, else UB.
> 
> hi, @kimbarrett, Thanks you for letting me know `alignof`.
> 
> I know the concept of 'nature alignment'. an object is aligned to its size. The rule is enforced by C/C++ compiler. Is it really UB if I violate it?
> 
> In this case, sizeof(Message) is 64. I don't think we have to put an instance of Message at an address which can be divided by 64. In particular, Message is POD. We would like to have compact 'buffer'.

Objects are not aligned to their size. Alignment and size are independent
properties of a type. The alignment of fundamental types is determined by ABI
and processor constraints. It's common for fundamental types to have alignment
requirements equal to their size. Compound types generally (barring use of
`alignas` specifiers and maybe other corner cases I'm forgetting) have the
maximum alignment of their components, to ensure the components are properly
aligned.  This may result in the compiler adding padding between class members.

And yes, misaligned accesses are UB.

BTW, `Message` is not POD.  It might be standard-layout (I haven't checked).

-------------

PR: https://git.openjdk.org/jdk/pull/11761


More information about the hotspot-dev mailing list