GCC __attribute __((排列(X)))的说明 [英] GCC __attribute__((aligned(x))) explanation

查看:347
本文介绍了GCC __attribute __((排列(X)))的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code:

#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] 不是 0×1000

究竟 __ __属性((排列(X)))呢?我误解了<一href="http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Variable-Attributes.html#Variable-Attributes">this如何解释呢?

What exactly __attribute__((aligned(x))) does? I misunderstood this explanation?

我用gcc 4.1.2。

I'm using gcc 4.1.2.

推荐答案

我认为,问题是,你的数组是在栈上。由于此功能时,堆栈​​指针可以是任何东西,也没有办法对准阵列未分配很多比你更需要调整它。如果移动阵列出来的功能,并成为一个全局变量,它应该工作。你可以做的另一件事是把它作为一个局部变量(这是一个非常好的事情),但要静态。这将prevent将其存储在堆栈上。要注意的是这两种方法是不是线程安全的或递归安全的,因为将有数组的一个副本。

I believe the problem is that your array is on the stack. Because the stack pointer could be anything when the function starts, there is no way to align the array without allocating a lot more than you need and adjusting it. If you move the array out of the function and into a global variable, it should work. The other thing you could do is keep it as a local variable (which is a very good thing), but make it static. This will prevent it from being stored on the stack. Beware that both of these ways are not thread-safe or recursion-safe, since there will be only one copy of the array.

通过这个code:

#include <stdio.h>

float a[4] __attribute__((aligned(0x1000))) = {1.0, 2.0, 3.0, 4.0};

int
main(void)
{
        printf("%p %p %p %p\n", &a[0], &a[1], &a[2], &a[3]);
}

我得到这样的:

I get this:

0x804c000 0x804c004 0x804c008 0x804c00c

这就是预期。随着原来的code,我只是得到随机值像你这样。

which is what is expected. With your original code, I just get random values like you did.

这篇关于GCC __attribute __((排列(X)))的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆