0

__STDCPP_DEFAULT_NEW_ALIGNMENT__ in MSVC is 16 while alignof(std::max_align_t) is 8. I read quite a few explainations and exiting questions and confirm that when override the new operators, we must make sure the allocated memory should align to __STDCPP_DEFAULT_NEW_ALIGNMENT__ at least (Why does the C++ standard allow std::max_align_t and __STDCPP_DEFAULT_NEW_ALIGNMENT__ to be inconsistent?). But if in this case, does it mean that the old code which overrides new operator should be modified? For example, I have a code which is compiled in C++11 and I make sure all memory allocated are aligned to alignof(std::max_align_t). Given the value of alignof(std::max_align_t) is 8 on MSVC, language version is C++11. If I should change the alignment to __STDCPP_DEFAULT_NEW_ALIGNMENT__ when I switches the compiler to C++17?

Each C++ language updates respect the old codes very much. It should not request to update code for fitting new standard. But I really not sure in this case. Because checking this ticket: Overloading operator new with smaller default alignment where mentions that

The reason I'm asking this, is because I'm investigating crashes in an MSVC program that is now being compiled with Clang. Here we noticed that clang uses CPU instructions that rely on this 16 bit alignment in order to initialize a class of size 8.

Looks like compiler is assuming the memory is 16 byte aligned. If in this case, my old code would crash due to the alignment issue.

So my question is: When I have C++11 codes if should upgrade the codes to make sure all customized memory allocator allocates memory align to __STDCPP_DEFAULT_NEW_ALIGNMENT__ when upgrade compiler to C++17?

Tinggo
  • 1,127
  • 1
  • 9
  • 18
  • Give some concrete [mre] in your question. I would suggest recompiling your C++ code with a recent [GCC](http://gcc.gnu.org/) invoked as `g++ -Wall -Wextra -g -O` or use the [clang static analyzer](https://clang-analyzer.llvm.org/) on it. Follow coding rules like [Misra c++](https://www.misra.org.uk/Activities/MISRAC/tabid/171/Default.aspx) and/or document your own ones – Basile Starynkevitch Feb 18 '21 at 08:02
  • Consider also writing your own [GCC plugin](https://gcc.gnu.org/onlinedocs/gccint/Plugins.html) to check some of your documented coding rules – Basile Starynkevitch Feb 18 '21 at 08:06

0 Answers0