warning C4316: object allocated on the heap may not be aligned 16

后端 未结 4 727
面向向阳花
面向向阳花 2020-12-30 01:16

Important Information:

  • Development OS: Windows 8.1 64 bit
  • Target OS: Windows 8.1 64 bit
  • IDE: Visual Studio 2013 Professional
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-30 02:11

    What does C4316 mean?

    C4316 is the error code. It's a unique identifier that makes it easy to find the documentation.

    What is causing it in my code?

    The usage of the DirectX::XMMATRIX class. Instances of that class must be aligned on 16-byte boundaries. The compiler makes sure that whenever you create a JGLib::Graphics::Direct3D instance on the stack or at global scope, it will align it properly, but if you allocate an instance dynamically on the heap, the compiler can't guarantee that the object will be aligned properly, because malloc() and friends typically only guarantee 8-byte alignment.

    What implications could this have in the future, if I ignore it?

    Your code may crash when accessing those matrix instances due to SSE instructions operating on misaligned data.

    How do I "fix" the problem that is causing this warning to appear?

    As the documentation suggests, you need to override your class's operator new and operator delete in order to guarantee 16-byte alignment. You can use _aligned_malloc() and _aligned_free() to allocate and free memory aligned on larger alignments.

提交回复
热议问题