重要信息:
- 开发操作系统:Windows 8.1 64 位
- 目标操作系统:Windows 8.1 64 位
- IDE:Visual Studio 2013 专业版
- 语言:C++
问题:
通过 IDE 编译我的静态库项目时收到以下警告:
warning C4316: ... : object allocated on the heap may not be aligned 16
我可以简单地忽略此警告...但我假设它的存在是有原因的,并且希望至少了解它的含义以及它对 future 可能产生的影响。
我认为这行代码与问题有关,在我的 Win32 窗口包装类中调用:
m_direct3D = new Direct3D(this);
m_direct3D 是一个指向我的 Direct3D 包装类的指针。
这是包装器的头文件(我承认它需要修剪):
#pragma once
// Windows
#include <d3d11.h>
#include <DirectXMath.h>
// Standard
#include <stdint.h>
#include <vector>
// JGlib
#include "Window.h"
namespace JGlib
{
namespace Graphics
{
class Direct3D
{
public:
// Construtor and destructor
Direct3D(const JGlib::Graphics::Window* window);
~Direct3D();
// Public methods
void Initialise();
void BeginDraw();
void Draw();
void EndDraw();
private:
// Private methods
// Private member variables
const Window* m_window;
ID3D11Device* m_device;
IDXGIAdapter* m_adapter;
DXGI_ADAPTER_DESC m_adapterDescription;
uint32_t m_videoCardMemory;
IDXGIFactory* m_factory;
IDXGIOutput* m_monitor;
DXGI_MODE_DESC* m_displayModes;
uint32_t m_numberOfModes;
DXGI_RATIONAL m_refreshRate;
DXGI_SWAP_CHAIN_DESC m_swapChainDescription;
D3D_FEATURE_LEVEL m_featureLevel;
ID3D11DeviceContext* m_deviceContext;
IDXGISwapChain* m_swapChain;
ID3D11Texture2D* m_backBuffer;
ID3D11RenderTargetView* m_renderTargetView;
ID3D11Texture2D* m_depthStencilBuffer;
D3D11_TEXTURE2D_DESC m_depthBufferDescription;
D3D11_DEPTH_STENCIL_DESC m_depthStencilDescription;
ID3D11DepthStencilState* m_depthStencilState;
ID3D11DepthStencilView* m_depthStencilView;
D3D11_RASTERIZER_DESC m_rasterDescription;
D3D11_VIEWPORT m_viewport;
float m_fieldOfView;
float m_screenAspectRatio;
ID3D11RasterizerState* m_rasterState;
DirectX::XMMATRIX m_projectionMatrix;
DirectX::XMMATRIX m_worldMatrix;
DirectX::XMMATRIX m_orthographicMatrix;
float m_screenDepth;
float m_screenNear;
};
}
}
我尝试用谷歌搜索该问题,但发现的信息很少。我发现的信息我不明白。
最后,我要问的是:
- C4316 是什么意思?
- 是什么导致我的代码出现这种情况?
- 如果我忽略它,这会对 future 产生什么影响?
- 如何“修复”导致出现此警告的问题?
其他信息:
当我将 Visual Studio 的配置管理器更改为针对 x64 进行编译时,不会出现此问题。
最佳答案
What does C4316 mean?
C4316 是错误代码。它是一个唯一标识符,便于查找 the documentation .
What is causing it in my code?
DirectX::XMMATRIX
类的用法。该类的实例必须在 16 字节边界上对齐。编译器确保每当您在堆栈或全局范围内创建 JGLib::Graphics::Direct3D
实例时,它都会正确对齐它,但如果您在堆上动态分配实例,则编译器不能保证对象会正确对齐,因为 malloc()
和 friend 通常只保证 8 字节对齐。
What implications could this have in the future, if I ignore it?
由于SSE,您的代码在访问这些矩阵实例时可能会崩溃操作未对齐数据的指令。
How do I "fix" the problem that is causing this warning to appear?
正如文档所建议的,您需要覆盖类的 operator new
和 operator delete
以保证 16 字节对齐。您可以使用_aligned_malloc()
和 _aligned_free()
分配和释放在较大对齐上对齐的内存。
关于c++ - 警告 C4316 : object allocated on the heap may not be aligned 16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20104815/