1

Under gcc4.9, the address obtained from the new operator is only 16-byte aligned, but under gcc5.1, it is 32-byte aligned.

I initially thought it was related to aligned new, but aligned new is only available when std=c++17 flag is used and compiled under gcc7.

The alignment rules of new should be controlled in Libstdc++, so I went to look at the code of libstdc++ , but did not find any changes related to gcc5

So my question is, why gcc5 can have 32 byte alignment, is there any report related to this?

111qqz
  • 11
  • 1
  • 1
    IIRC, since C++17, `new` expression (and `operator new` allocation function cannot handle) over-aligned types. Even with GCC 5.1, `alignof(std::max_align_t)` is 16, not 32: https://godbolt.org/z/95Y4TsaP5. Your observation may be simply conincidence. – Daniel Langr Jan 04 '22 at 12:33
  • If 16-byte-aligned allocations are randomly distributed, some of them will happen to also be 32-byte aligned by chance. I guess you're actually talking about cases like the one you linked, where you're using it on an `alignas(32)` type, so C++17 would I think require it to actually respect that alignment for dynamic allocation if you use `-std=gnu++17`. You only make one `new` call in your program so it's not much of a test. – Peter Cordes Jan 04 '22 at 12:33
  • See also [How to solve the 32-byte-alignment issue for AVX load/store operations?](https://stackoverflow.com/q/32612190) – Peter Cordes Jan 04 '22 at 12:36
  • @DanielLangr Thanks for your answer, I know that alignof(std::max_align_t) is 16 under gcc5.1, which is the same as under gcc4.9. But under gcc5.1, the results of running hundreds of tests are all 32 byte aligned. Doesn't look like a coincidence – 111qqz Jan 04 '22 at 12:51
  • @PeterCordes Thanks for answering. I understand your considerations. In fact, I have conducted hundreds of tests, and the results under Gcc5.1 are all aligned with 32 bytes. The link to godbolt is just to show the tested code – 111qqz Jan 04 '22 at 12:53
  • 2
    What is the version of glibc are you using? `alignment rules of new should be controlled in Libstdc++`? It's just calling malloc, like [here](https://gcc.gnu.org/git/?p=gcc-old.git;a=blob;f=libstdc%2B%2B-v3/libsupc%2B%2B/new_op.cc;h=c445a105b5a4ef101258068f2d55a66944f9f614;hb=HEAD#l50). – KamilCuk Jan 04 '22 at 13:05
  • @KamilCuk The glibc version of gcc 4.9 and gcc 5 are both 2.19. But the versions of libstdc++ are 6.0.20 and 6.0.21 respectively. Then according to this [post](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=15795), glibc's malloc will not deal with the alignment problem, it should be handled by libstdc++ when calling malloc. – 111qqz Jan 05 '22 at 02:09

0 Answers0