C标准是否要求n个元素的数组的大小是元素大小的n倍? [英] Does the C standard require the size of an array of n elements to be n times the size of an element?

查看:109
本文介绍了C标准是否要求n个元素的数组的大小是元素大小的n倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C标准是否要求通过显式语句或通过严格的逻辑推导,将 n 个元素的数组的大小 n 乘以元素的大小?它的要求?

例如,int (*x)[5] = malloc(5 * sizeof **x);可能无法为包含五个int的数组请求足够的空间吗?

C 2011 [N1570] 6.5.3.4 7显示了将数组中元素的数量计算为sizeof array / sizeof array[0]的示例.但是,示例不是该标准的规范性部分(根据前瞻性第8段).

6.2.5 20说数组类型描述了具有特定类型的连续分配的非空对象集,但对所需的总内存没有任何提示.

这仅仅是一个语言律师问题;实际的实现是无关紧要的. (为安抚那些想要具体示例的人,假设一个C实现需要对大型数组进行额外的内存管理,因此创建数组需要创建一些其他数据来帮助管理内存.)

解决方案

描述数组表示形式的唯一文本非常简洁,它位于6.2.5¶20中:

可以从对象和函数类型构造任意数量的派生类型,如下所示:

  • 数组类型描述具有特定成员对象类型(称为元素类型)的连续分配的非空对象集.只要指定了数组类型,元素类型就应该是完整的.数组类型通过其元素类型和数组中元素的数量来表征.数组类型据说是从其元素类型派生的,如果其元素类型为T,则有时将数组类型称为"T的数组".从元素类型构造数组类型的过程称为数组类型派生".

请注意,它不会说连续分配对象的非空集和填充"之类的内容,因此数组只是对象.因此,似乎没有理由声称数组[type]可以产生除连续对象集的大小以外的任何结果,这显然是单个元素类型的大小的N倍. /p>

还值得注意的是,填充并不是仅凭其自身就可以存在的东西,因为未指定填充不存在. C指定类型的表示形式(6.2.6),并在适当的地方明确指定填充位和字节的可能性.没有关于数组填充的文字,因此它不是数组表示的一部分.

Does the C standard require that the size of an array of n elements be n times the size of an element, either by explicit statement or by rigorous logical deduction from its requirements?

For example, could int (*x)[5] = malloc(5 * sizeof **x); fail to request sufficient space for an array of five int?

C 2011 [N1570] 6.5.3.4 7 shows an example of computing the number of elements in an array as sizeof array / sizeof array[0]. However, examples are not a normative part of the standard (per paragraph 8 of the forward).

6.2.5 20 says an array type describes a contiguously allocated nonempty set of objects with a particular type but is silent about the total memory required.

This is solely a language-lawyer question; actual implementations are irrelevant. (To appease those who want concrete examples, hypothesize a C implementation that requires additional memory management for large arrays, so creating an array requires creating some additional data to help manage the memory.)

解决方案

The only text describing the representation of arrays is quite terse, and is in what you found at 6.2.5 ¶20:

Any number of derived types can be constructed from the object and function types, as follows:

  • An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The element type shall be complete whenever the array type is specified. Array types are characterized by their element type and by the number of elements in the array. An array type is said to be derived from its element type, and if its element type is T , the array type is sometimes called ''array of T ''. The construction of an array type from an element type is called ''array type derivation''.

Note that it does not say something like "contiguously allocated nonempty set of objects and padding", so the array is just the objects. Thus there seems to be no basis for a claim that sizeof the array [type] could yield any result other than the size of the contiguous set of objects, which is obviously N times the size of the individual element type.

It's also worth noting that padding is not something that can just exist on its own because it's not specified not to exist. C specifies representations of types (6.2.6) and explicitly specifies the possibility of padding bits and bytes where appropriate. There is no text about padding for arrays, and thus it's not part of their representation.

这篇关于C标准是否要求n个元素的数组的大小是元素大小的n倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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