如何对齐 Windows x64 C++ 全局变量的内存地址

huangapple go评论65阅读模式
英文:

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

问题

我希望我创建的全局变量的地址是0x1000的倍数。

示例

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

输出

"var address = [0x140005000]"
英文:

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]"

答案1

得分: 2

只使用 alignas

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

Just use alignas:

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

huangapple
  • 本文由 发表于 2023年6月18日 17:41:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76499895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定