基于相同的对齐方式和/或表示方式,一个对象的大小是否等于另一个对象的大小? [英] Is the size of an object equivalent to the size of another based upon the same alignment and/or representation?

查看:61
本文介绍了基于相同的对齐方式和/或表示方式,一个对象的大小是否等于另一个对象的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C标准规定(强调我的意思):

28指向 void 的指针应具有与字符类型的指针相同的表示和对齐要求. 48)类似地,指针兼容类型的合格或不合格版本应具有相同的表示和对齐要求.所有指向结构类型的指针应具有相同的表示和对齐要求.指向联合类型的所有指针应具有相同的表示和对齐要求.指向其他类型的指针不必具有相同的表示或对齐要求.

48)相同的表示形式和对齐要求旨在隐含与函数的参数,函数的返回值以及并集成员的互换性.

来源:C11,§6.2.5/28

"相同表示和对齐"的措词经常在这里发生.

但是相同的大小呢?

我不知道这些指针对象之间在分配大小方面是否存在差异,因为即使对齐方式和表示形式相同,指针对象的大小也会在所指向的类型之间变化.

或者换句话说:是否可以保证如果对齐方式和/或表示形式相等,则大小也是如此?

问题:

  • 基于相同的对齐方式和/或表示方式,一个对象的大小是否等于另一个对象的大小?

注释:

  • 问题不仅仅针对指针对象.指针指导只是对我心态的一种参考,因为它是一个很好的例子.

  • 该标准的引文受到高度赞赏.接受的答案必须带有标准的引号.

  • 设置是完全相同的特定实现.我不会谈论不同实现之间对象的各种对齐方式/表示形式/大小.


相关(关于指针示例):

解决方案

C 2018第6.2.6节标题为类型的表示形式",指定类型的表示形式.第2段说:

除位域外,对象由一个或多个字节的连续序列组成,其编号,顺序和编码均明确指定或由实现定义.

由此可见,对象的表示形式是字节序列,并且该序列具有一定数量的字节,某种顺序和某种编码.因此,字节数,字节顺序和字节编码是表示的一部分.因此,如果两个对象具有相同的表示形式,则它们具有相同的字节数,相同的顺序和相同的编码.

因为它们具有相同的字节数,所以它们具有相同的大小.

例如,如果对象X用字节A,B和C表示,而对象Y用字节A,B,C和D表示,则X和Y的表示方式不同./p>

The C standard states (emphasize mine):

28 A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.

48) The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.

Source: C11, §6.2.5/28

The wording of "the same representation and alignment" happens here often.

But what about the same size?

I wonder if there can be a difference between between these pointer objects in terms of the allocated size since the size of a pointer object can vary between the type pointed to even if the alignment and representation is the same.

Or in other words: Is there a guarantee that if the alignment and/or representation is equal, the size is so too?

Question:

  • Is the size of an object equivalent to the size of another based upon the same alignment and/or representation?

Annotations:

  • The question is not specific to pointer objects only. The pointer guidance was just a reference to my mindset as it is a good example.

  • Citations from the standard are highly appreciated. Accepted answer must have quotations from the standard.

  • Setting is the exact same specific implementation. I don't talk about various alignments/representations/sizes of objects between different implementations.


Related (regarding the pointer example):

解决方案

C 2018 clauses 6.2.6, entitled "Representations of types," specifies representations of types. Paragraph 2 says:

Except for bit-fields, objects are composed of contiguous sequences of one or more bytes, the number, order, and encoding of which are either explicitly specified or implementation-defined.

From this, it is clear that a representation of an object is a sequence of bytes, and that sequence has some number of bytes, some order, and some encoding. So the number of bytes, the order of the bytes, and the encoding of the bytes is part of the representation. Therefore, if two objects have the same representation, they have the same number of bytes, the same order, and the same encoding.

Since they have the same number of bytes, they have the same size.

As an example, if an object X is represented with bytes A, B, and C, and object Y is represented with bytes A, B, C, and D, then X and Y do not have the same representation.

这篇关于基于相同的对齐方式和/或表示方式,一个对象的大小是否等于另一个对象的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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