inf*_*ero 5 c++ memory-management memory-alignment new-operator delete-operator
在我的矩阵类中,我按如下方式分配对齐内存:
/*** main.cpp ***/
#include "matrix.hpp"
int main()
{
{
Matrix(15, 17);
}
return 0;
}
/** matrix.hpp **/
class Matrix
{
private:
std::size_t width_, height_;
double* data_;
public:
Matrix(std::size_t width, std::size_t height);
~Matrix();
};
/** matrix.cpp **/
Matrix::Matrix(std::size_t width, std::size_t height)
: width_(width)
, height_(height)
{
data_ = new(std::align_val_t{64}) double[width_ * height_];
}
Run Code Online (Sandbox Code Playgroud)
我该如何正确删除它?
我都尝试过
Matrix::~Matrix()
{
delete[] data_;
}
Run Code Online (Sandbox Code Playgroud)
和
Matrix::~Matrix()
{
delete[](std::align_val_t{64}, data_);
}
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
SIGTRAP (Trace/breakpoint trap)
ntdll!RtlIsNonEmptyDirectoryReparsePointAllowed 0x00007ffe5dc390e3
ntdll!RtlpNtMakeTemporaryKey 0x00007ffe5dc41512
ntdll!RtlpNtMakeTemporaryKey 0x00007ffe5dc4181a
ntdll!RtlpNtMakeTemporaryKey 0x00007ffe5dc4a7d9
ntdll!RtlGetCurrentServiceSessionId 0x00007ffe5db8081d
ntdll!RtlFreeHeap 0x00007ffe5db7fba1
msvcrt!free 0x00007ffe5bc09cfc
Matrix::~Matrix matrix.cpp:15
main main.cpp:16
__tmainCRTStartup 0x00000000004013c1
mainCRTStartup 0x00000000004014f6
Run Code Online (Sandbox Code Playgroud)
其中第 15 行指的是包含删除[] 的行。
我在 Windows10 上使用 MSys2 的 g++ 10.1。
编辑:如果我在 WSL 下使用 g++ 10.1 编译它,它就可以正常工作。所以这可能是由 Windows 端口引起的。