我有以下代码:
#include <stdio.h>
int
main(void)
{
float a[4] __attribute__((aligned(0x1000))) = {1.0, 2.0, 3.0, 4.0};
printf("%p %p %p %p\n", &a[0], &a[1], &a[2], &a[3]);
}
我有以下输出:
0x7fffbfcd2da0 0x7fffbfcd2da4 0x7fffbfcd2da8 0x7fffbfcd2dac
为什么a[0]
的地址不是0x1000
的倍数
__attribute__((aligned(x)))
到底做了什么?我误解了this的解释?
我用的是gcc 4.1.2。
发布于 2011-05-04 10:36:17
最近的GCC (使用4.5.2-8ubuntu4测试)在数组正确对齐的情况下似乎可以正常工作。
#include <stdio.h>
int main(void)
{
float a[4] = { 1.0, 2.0, 3.0, 4.0 };
float b[4] __attribute__((aligned(0x1000))) = { 1.0, 2.0, 3.0, 4.0 };
float c[4] __attribute__((aligned(0x10000))) = { 1.0, 2.0, 3.0, 4.0 };
printf("%p %p %p %p\n", &a[0], &a[1], &a[2], &a[3]);
printf("%p %p %p %p\n", &b[0], &b[1], &b[2], &b[3]);
printf("%p %p %p %p\n", &c[0], &c[1], &c[2], &c[3]);
}
我得到了:
0x7ffffffefff0 0x7ffffffefff4 0x7ffffffefff8 0x7ffffffefffc
0x7ffffffef000 0x7ffffffef004 0x7ffffffef008 0x7ffffffef00c
0x7ffffffe0000 0x7ffffffe0004 0x7ffffffe0008 0x7ffffffe000c
https://stackoverflow.com/questions/841433
复制相似问题