Search code examples
c++memory-addressmemory-alignment

How to align memory address of Windows x64 C++ global variable


I want the address of the global variable I create to be a multiple of 0x1000.

sample

char alignstr[0x100] = "this is align string";
int main()
{
    printf("var address = [%p]", &alignstr);
}

output

"var address = [0x140005000]"

Solution

  • Just use alignas:

    alignas(0x1000) char alignstr[0x100] = "this is align string";