0

I use the following to 16-byte align a 4 element float array in a class

class Foo
{
    // ... some code

    private:
    alignas(16) float array[4];
};

The variable is not aligned. Why is that? My assumption would be that the alignment depends on the alignment of the instance of Foo since the internal offset of member variables is static and not adjusted on runtime. Is that correct or am I missing something? Thus one thing to fix it would be to align Foo with something >=16 so that the internal offsets are fixed?

ruhig brauner
  • 943
  • 1
  • 13
  • 35
  • 2
    how do you know that it is not aligned? What do you mean exactly when you say "The variable is not aligned." ? – 463035818_is_not_an_ai Jun 25 '19 at 14:41
  • 1
    `alignas(16) float array[4];` should work. Can you show us a [mre] that doesn't? – NathanOliver Jun 25 '19 at 14:45
  • OK,then my assumption already seems to be wrong. I can't give you a minimal example. I'll try to create one but I assume it'll simply work if that's what it's supposed to do. After construction of Foo, array is pointing to a 8 byte aligned address, the last hex-digit is 8 instead of 0. – ruhig brauner Jun 25 '19 at 14:53
  • Foo is created dynamically with new, if that is important for this example. – ruhig brauner Jun 25 '19 at 15:00
  • Ok, `alignof(std::max_align_t);` returns 8. That afaik means that the compiler doesn't guarantee that alignment over 8 bytes (in dynamic allocations?) is supported. – ruhig brauner Jun 25 '19 at 15:07
  • Is there any conclusion here? I also confused in this issue that array somehow aligns to 8 even if I specify the alignment explicitly like what ruhig did. – Tinggo Apr 14 '20 at 11:33

0 Answers0