錯誤: invalid-allocation-alignment

位址清理程式錯誤:invalid-allocation-alignment

_aligned_malloc 式需要 2 的電源來表示對齊方式。 我們會使用未優化全域變數來模擬某些對齊因數的「外部」計算。

範例

// example1.cpp
// invalid-allocation-alignment error
#include <Windows.h>

int ExternalAlign = 5;

int main(){

    // this externally calculated alignment of 5 isn't valid.

    void* ptr = _aligned_malloc(8,ExternalAlign); 
    return (ptr == nullptr && errno == EINVAL) ? 0 : -1;
}

若要建置及測試此範例,請在 Visual Studio 2019 16.9 版或更新版本的 開發人員命令提示字元 中執行下列命令:

cl example1.cpp /fsanitize=address /Zi
devenv /debugexe example1.exe

產生的錯誤

Screenshot of debugger displaying invalid-allocation-alignment error in example 1.

另請參閱

AddressSanitizer 概觀
AddressSanitizer 已知問題
AddressSanitizer 組建和語言參考
AddressSanitizer 執行時間參考
AddressSanitizer 陰影位元組
AddressSanitizer 雲端或分散式測試
AddressSanitizer 偵錯工具整合
AddressSanitizer 錯誤範例