Skip to content

Latest commit

 

History

History
52 lines (38 loc) · 1.92 KB

error-invalid-allocation-alignment.md

File metadata and controls

52 lines (38 loc) · 1.92 KB
title description ms.date f1_keywords helpviewer_keywords
Error: invalid-allocation-alignment
Source examples and live debug screenshots for invalid _aligned_malloc errors.
03/02/2021
invalid-allocation-alignment
invalid-allocation-alignment error
AddressSanitizer error invalid-allocation-alignment

Error: invalid-allocation-alignment

Address Sanitizer Error: invalid-allocation-alignment

The _aligned_malloc function requires a power of 2 for expressing the alignment. We simulate the "external" calculation of some alignment factor using an unoptimized global variable.

Example

// 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;
}

To build and test this example, run these commands in a Visual Studio 2019 version 16.9 or later developer command prompt:

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

Resulting error

:::image type="content" source="media/invalid-allocation-alignment-example-1.png" alt-text="Screenshot of debugger displaying invalid-allocation-alignment error in example 1.":::

See also

AddressSanitizer overview
AddressSanitizer known issues
AddressSanitizer build and language reference
AddressSanitizer runtime reference
AddressSanitizer shadow bytes
AddressSanitizer cloud or distributed testing
AddressSanitizer debugger integration
AddressSanitizer error examples