char array as storage for placement new

后端 未结 5 1646
[愿得一人]
[愿得一人] 2020-12-31 16:43

Is the following legal C++ with well-defined behaviour?

class my_class { ... };

int main()
{
    char storage[sizeof(my_class)];
    new ((void *)storage) m         


        
5条回答
  •  我在风中等你
    2020-12-31 17:09

    It is at least problematic due to alignment.

    On most Non-Intel architecture the code will generate a "bus error" due to wrong alignment or be extremely slow because of processor traps needed to fix the unaligned memory access.

    On Intel architecture this will normally just be a bit slower than usual. Except if some SSE operations are involved, then it may also crash.

提交回复
热议问题