我用CreateFile2和WriteFile創建和寫入一個文件,然後使用readfile一次將16個字節讀入__m128i,然後對其執行simd操作。在調試模式下工作正常,但在發佈模式下拋出訪問被拒絕(0xc0000005)錯誤代碼。根據我的經驗,當我嘗試將非16字節對齊的東西推入16字節對齊的東西時會發生這種情況。但是,我不確定缺少16字節對齊的地方是不是首先要養成它醜陋的頭腦。CreateFile2,WriteFile和ReadFile:如何強制執行16個字節的對齊?
#define simd __m128i
它是否在CreateFile2()調用?
_CREATEFILE2_EXTENDED_PARAMETERS extend = { 0 };
extend.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
extend.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
extend.dwFileFlags = /*FILE_FLAG_NO_BUFFERING |*/ FILE_FLAG_OVERLAPPED;
extend.dwSecurityQosFlags = SECURITY_ANONYMOUS;
extend.lpSecurityAttributes = nullptr;
extend.hTemplateFile = nullptr;
hMappedFile = CreateFile2(
testFileName.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
OPEN_ALWAYS,
&extend);
...在WriteFile()調用中?
_OVERLAPPED positionalData;
positionalData.Offset = 0;
positionalData.OffsetHigh = 0;
positionalData.hEvent = 0;
bool writeCheck = WriteFile(
hMappedFile,
&buffer[0],
vSize,
NULL,
&positionalData);
...在稍後的ReadFile()調用中?
const simd* FileNodePointer(
_In_ const uint32_t index) const throw()
{
std::vector<simd> Node(8);
_OVERLAPPED positionalData;
positionalData.Offset = index;
positionalData.OffsetHigh = 0;
positionalData.hEvent = 0;
ReadFile(
hMappedFile,
(LPVOID)&Node[0],
128,
NULL,
&positionalData);
return reinterpret_cast<const simd*>(&Node[0]);
}
如何在這裏執行16字節對齊?
謝謝!
你的調試器告訴你什麼?從事故現場後退工作,找出問題的起因。 – 1201ProgramAlarm
擺脫轉換,隱式轉換就足夠了,reinterpret_cast是危險的,編譯器更容易在代碼簡化時發現生命期錯誤。 –