C数组可包含的元素之间进行填充? [英] Can C arrays contain padding in between elements?

查看:164
本文介绍了C数组可包含的元素之间进行填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说,在C,所包含的内部结构阵列可能会添加填充数组中的元素之间的传闻。现在很明显,填充量不能任意对元素或计算数组的下一个元素之间的变化是不可能的,简单的指针运算。

I heard a rumor that, in C, arrays that are contained inside structs may have padding added in between elements of the array. Now obviously, the amount of padding could not vary between any pair of elements or calculating the next element in an array is not possible with simple pointer arithmetic.

这传闻还指出,这是不包含在结构数组都保证不含填充。我知道至少这部分是真实的。

This rumor also stated that arrays which are not contained in structures are guaranteed to contain no padding. I know at least that part is true.

所以,在code,传闻是:

So, in code, the rumor is:

{
    // Given this:
    struct { int values[20]; } foo;
    int values[20];

    // This may be true:
    sizeof(values) != sizeof(foo.values);
}

我是pretty肯定的是,的sizeof(值)将始终等于的sizeof(foo.values​​)。不过,我一直无法找到C标准(特别是C99)任何明确证实或否认这一点。

I'm pretty certain that sizeof(values) will always equal sizeof(foo.values). However, I have not been able to find anything in the C standard (specifically C99) that explicitly confirms or denies this.

有谁知道这个传闻是任何C标准的解决?

Does anyone know if this rumor is addressed in any C standard?

修改:据我了解,有可能是结构<$ C末端的数组末尾之间的填充 foo.values​​ 和$ C>富和标准的规定,将有富和> foo.values​​ 。然而,没有任何人有从或参考的地方说,有一种元素之间没有填充标准 foo.values​​ 报价C $ C>?

edit: I understand that there may be padding between the end of the array foo.values and the end of the struct foo and that the standard states that there will be no padding between the start of foo and the start of foo.values. However, does anyone have a quote from or reference to the standard where it says there is no padding between the elements of foo.values?

推荐答案

没有,也绝不会是一个数组元素之间的填充英寸这是专门不允许的。 C99标准调用数组类型数组类型描述了连续分配的非空的对象集...。对于此相反,结构是顺序,而不是连续分配的。

No, there will never be padding in between elements of an array. That is specifically not allowed. The C99 standard calls array types "An array type describes a contiguously allocated nonempty set of objects...". For contrast, a structure is "sequentially", not "contiguously" allocated.

有可能是之前或结构内的阵列后填充;这完全是另一种动物。编译器可能会做,以帮助结构对齐,但C标准不说任何东西。

There might be padding before or after an array within a structure; that is another animal entirely. The compiler might do that to aid alignment of the structure, but the C standard doesn't say anything about that.

这篇关于C数组可包含的元素之间进行填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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