首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >阵列的内存对齐

阵列的内存对齐
EN

Stack Overflow用户
提问于 2014-04-29 14:43:25
回答 3查看 24.3K关注 0票数 9

对于单元处理器上的DMA传输,我很难对齐内存。我需要地址的最后4位是0。

我有4个unsigned int数组,每个元素必须在内存中对齐,以便其(十六进制)入口以零结尾。

例如。

代码语言:javascript
复制
int main()
{
    size_t i;

    static unsigned int a[2] __attribute__ ((aligned (16)));
    static unsigned int b[2] __attribute__ ((aligned (16)));
    static unsigned int c[2] __attribute__ ((aligned (16)));
    static unsigned int d[2] __attribute__ ((aligned (16)));

    for (i = 0; i < 2; ++i) {
        printf("a[%u] = %p\n", &a[i]);
        printf("b[%u] = %p\n", &b[i]);
        printf("c[%u] = %p\n", &c[i]);
        printf("d[%u] = %p\n", &d[i]);
    }

    return 0;
}

输出:

代码语言:javascript
复制
a[0] = 0x10010b60
b[0] = 0x10010b50
c[0] = 0x10010b40
d[0] = 0x10010b30
a[1] = 0x10010b64
b[1] = 0x10010b54
c[1] = 0x10010b44
d[1] = 0x10010b34

这里的问题是,每个数组的第二个元素似乎没有对齐16位(它们的地址‘以4结尾)。

我需要这样的地址:

代码语言:javascript
复制
a[0] = 0xXXXXXXX0
b[0] = 0xXXXXXXX0
c[0] = 0xXXXXXXX0
d[0] = 0xXXXXXXX0
a[1] = 0xXXXXXXX0
b[1] = 0xXXXXXXX0
c[1] = 0xXXXXXXX0
d[1] = 0xXXXXXXX0
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-04-29 15:33:20

对齐属性指定变量或结构字段的对齐方式,而不是单个数组元素。详细信息请参见指定变量的属性公共变量属性

如果您总是希望将两个整数对齐,则可以定义结构。

代码语言:javascript
复制
struct dma_transfer {
    unsigned int e0 __attribute__ ((aligned (16)));
    unsigned int e1 __attribute__ ((aligned (16)));
};

这将在16个字节边界上对齐元素。

代码语言:javascript
复制
int main(int argc, char **argv)
{
    static struct dma_transfer a;
    static unsigned int b[2];

    printf("a.e0 = %p\n", &a.e0);
    printf("a.e1 = %p\n", &a.e1);
    printf("b[0] = %p\n", &b[0]);
    printf("b[1] = %p\n", &b[1]);

    return 0;
}

给予,例如。

代码语言:javascript
复制
a.e0 = 0x601060
a.e1 = 0x601070
b[0] = 0x601080
b[1] = 0x601084

但这也意味着,在两个整数值之间有洞。在32位系统上,您将拥有

int 4字节/孔12字节 int 4字节/孔12字节

票数 11
EN

Stack Overflow用户

发布于 2014-04-29 15:45:02

如果arr是由32位元素组成的数组,而arr[0]的地址是0xXXXXXXX0,那么arr[1]的地址必然是0xXXXXXXX4

为此,您需要使用由16字节元素组成的数组:

代码语言:javascript
复制
typedef struct
{
    unsigned int x;
    unsigned char reserved[16-sizeof(unsigned int)];
}
element_t;

static element_t a[2] __attribute__ ((aligned (16)));
static element_t b[2] __attribute__ ((aligned (16)));
static element_t c[2] __attribute__ ((aligned (16)));
static element_t d[2] __attribute__ ((aligned (16)));

或者,您可以完全不使用数组。

相反,使用普通变量,并告诉编译器将它们对齐到16个字节:

代码语言:javascript
复制
static unsigned int a0 __attribute__ ((aligned (16)));
static unsigned int a1 __attribute__ ((aligned (16)));
static unsigned int b0 __attribute__ ((aligned (16)));
static unsigned int b1 __attribute__ ((aligned (16)));
static unsigned int c0 __attribute__ ((aligned (16)));
static unsigned int c1 __attribute__ ((aligned (16)));
static unsigned int d0 __attribute__ ((aligned (16)));
static unsigned int d1 __attribute__ ((aligned (16)));
票数 6
EN

Stack Overflow用户

发布于 2014-04-29 14:46:51

我真的不认为你能做到..。您正试图让编译器“在”unsigned int中为别名目的注入额外的填充。但是没有空间可以这样做,unsigned int中的所有位都已经用于整数本身。

我认为解决方案是将整数封装在一个结构中,因为这样您就可以在结构上使用__attribute__(())魔术,并生成一个数组。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23367937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档