结构/对象内的存储顺序 [英] Order of storage inside a structure / object

查看:194
本文介绍了结构/对象内的存储顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下两种情况:

struct customType
{
   dataType1 var1; 
   dataType2 var2;
   dataType3 var3;
} ;

customType instance1;
// Assume var1, var2 and var3 were initialized to some valid values.

customType * instance2 = &instance1;    
dataType1 firstMemberInsideStruct = (dataType1)(*instance2);







class CustomType
{
   public:
       dataType1 member1;
       dataType2 member2;

       retrunType1 memberFunction1();

   private:
       dataType3 member3;
       dataType4 member4;

       retrunType2 memberFunction2();
};

customType object;
// Assume member1, member2, member3 and member4 were initialized to some valid values.

customType *pointerToAnObject = &object ;
dataType1 firstMemberInTheObject = (dataType1) (*pointerToAnObject);

这样做是否安全?

我想知道标准是否指定任何存储顺序 -

I want to know if standard specifies any order of storage among -



  1. C ++类的对象内的数据成员。


推荐答案

C99和C ++在此方面有所不同。

C99 and C++ differ a bit on this.

C99标准保证结构的字段将按照它们被声明的顺序布置在内存中,并且两个相同结构的字段将具有相同的偏移。有关C99标准的相关章节,请参见此问题。总结:第一个字段的偏移量指定为零,但是之后的偏移量不是由标准指定的。这是为了允许C编译器调整每个字段的偏移,以便该字段将满足该架构的任何存储器对齐要求。因为这是实现相关的,C提供了一种标准方法来确定每个字段的偏移量,使用 offsetof 宏。

The C99 standard guarantees that the fields of a struct will be laid out in memory in the order they are declared, and that the fields of two identical structs will have the same offsets. See this question for the relevant sections of the C99 standard. To summarize: the offset of the first field is specified to be zero, but the offsets after that are not specified by the standard. This is to allow C compilers to adjust the offsets of each field so the field will satisfy any memory alignment requirements of the architecture. Because this is implementation-dependent, C provides a standard way to determine the offset of each field using the offsetof macro.

C ++仅为普通旧数据(POD)。不是普通旧数据的C ++类不能像这样处理。该标准给了C ++编译器在类使用多继承,具有非公共字段或成员或包含虚拟成员时如何组织类的自由。

C++ offers this guarantee only for Plain old data (POD). C++ classes that are not plain old data cannot be treated like this. The standard gives the C++ compiler quite a bit of freedom in how it organizes a class when the class uses multiple inheritance, has non-public fields or members, or contains virtual members.

这对您的示例意味着什么:

What this means for your examples:

dataType1 firstMemberInsideStruct = (dataType1)(*instance2);

只有 dataType1,dataType2和dataType3是纯的旧数据。如果任何一个没有,那么customType结构体可能没有一个简单的构造函数(或析构函数),这个假设可能不成立。

This line is okay only if dataType1, dataType2, and dataType3 are plain old data. If any of them are not, then the customType struct may not have a trivial constructor (or destructor) and this assumption may not hold.

dataType1 firstMemberInTheObject = (dataType1) (*pointerToAnObject);

此行不安全,不管 dataType1 dataType2 dataType3 都是POD,因为 CustomType 类有私有实例变量。这使得它不是一个POD类,因此你不能假设它的第一个实例变量将以特定的方式排序。

This line is not safe regardless of whether dataType1, dataType2, and dataType3 are POD, because the CustomType class has private instance variables. This makes it not a POD class, and so you cannot assume that its first instance variable will be ordered in a particular way.

这篇关于结构/对象内的存储顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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