You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

如何让全局变量的内存地址为0x1000的整数倍?

要实现全局变量地址按0x1000(即4KB)对齐,你可以直接利用编译器提供的对齐属性强制指定对齐规则,不同编译器的写法略有区别:

GCC/Clang 编译器写法

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

#include <stdio.h>

int main()
{
    printf("var address = [%p]", &alignstr);
    return 0;
}

MSVC 编译器写法

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

#include <stdio.h>

int main()
{
    printf("var address = [%p]", &alignstr);
    return 0;
}

运行后输出示例:

var address = [0x140005000]

可以看到输出的地址0x140005000是0x1000的整数倍(0x140005000 ÷ 0x1000 = 0x140005,无余数),完全符合需求。

内容的提问来源于stack exchange,提问作者i like cat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 13:52:38