如何是一类的内存布局与结构体 [英] How is the memory layout of a class vs. a struct

查看:139
本文介绍了如何是一类的内存布局与结构体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自C编程其中,在结构中的数据与前第一可变布局,然后是第二,第三等..

我现在的编程C ++和我使用的是类代替。我基本上要达到相同的,但我也想get / set方法,也可能是其它方法(我也想尝试做一个C ++风格和马爷学习新的东西)。

有保证例如公众变量将先在内存中,然后私有变量?


解决方案

  

有保证例如公众变量将先在
  内存则私有变量?


没有,这样的保证是的不可以制作 - C ++ 11标准,[class.mem] / 14:


  

相同的访问(非工会)类的非静态数据成员
  控制
(第11)分配以至于后来的成员有较高
  一类对象中的地址。 非静态分配的顺序
  具有不同的访问控制数据成员是不确定
(11)。


所以

 一个结构
{
    INT I,J;
    性病::字符串str;私人的:    浮F;保护:    双D;
};

这只能保证,对于类型的给定对象 A


  • I 比更小的地址Ĵ

  • Ĵ比更小的地址 STR

需要注意的是类键结构有没有关于差别布局凡:他们唯一的区别是访问-rights只存在在编译时。



  

这只是说的顺序,但不是第一个变量真正开始
  在首地址?让我们假设一个类继承没有


是的,但仅限于标准布局类。还有就是要求类必须满足以下是一个标准布局类,其中之一是,所有成员都具有相同的访问控制的行。结果
引用C ++ 14(同样适用于C ++ 11,但措辞更为间接的),[class.mem] / 19:


  

如果一个标准的布局类对象具有任何非静态数据成员,其
  地址是相同的其第一非静态数据的地址
  会员。否则,它的地址是相同的它的第一个的地址
  基classsubobject(如有的话)。 [注意的:有可能因此受到
  根据需要一个标准布局结构对象内无名填充,但不是在它的开头,以实现适当的对准。 - 注完的]


[类] / 7:


  

一个的标准布局类的是一类:


  
  

      
  • 有型非标准布局类的没有非静态数据成员(或这些类型的数组)或引用,

  •   
  • 没有虚函数(10.3),没有虚基类(10.1),

  •   
  • 有相同的访问控制(第11),所有的非静态数据成员,

  •   
  • 没有非标准布局基类,

  •   
  • 或者具有在最派生类,并在与非静态数据成员最多有一个基类没有非静态数据成员,或者没有基
      类与非静态数据成员,以及

  •   
  • 具有相同的类型与第一非静态数据成员的无基类。 110

  •   

  
  

110)这确保两个子对象具有相同的类型,而且属于同一最派生的对象是不
  在同一个地址(5.10)。

分配

I come from C programming where the data in a struct is laid out with the top variable first, then the second, third and so on..

I am now programming in C++ and I am using a class instead. I basically want to achieve the same, but I also want get/set methods and also maybe other methods (I also want to try do it in a C++ style and maye learn something new).

Is there a guarantee e.g. that the public variables will be first in memory then the private variable?

解决方案

Is there a guarantee e.g. that the public variables will be first in memory then the private variable?

No, such a guarantee is not made - C++11 standard, [class.mem]/14:

Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (11).

So

struct A
{
    int i, j;
    std::string str;

private:

    float f;

protected:

    double d;
};

It is only guaranteed that, for a given object of type A,

  • i has a smaller address than j and
  • j has a smaller address than str

Note that the class-keys struct and class have no difference regarding layout whatsoever: Their only difference are access-rights which only exist at compile-time.


It only says the order, but not that the first variable actually start at the "first address"? Lets assume a class without inheritance.

Yes, but only for standard-layout classes. There is a row of requirements a class must satisfy to be a standard-layout class, one of them being that all members have the same access-control.
Quoting C++14 (the same applies for C++11, but the wording is more indirect), [class.mem]/19:

If a standard-layout class object has any non-static data members, its address is the same as the address of its first non-static data member. Otherwise, its address is the same as the address of its first base classsubobject (if any). [ Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment. — end note ]

[class]/7:

A standard-layout class is a class that:

  • has no non-static data members of type non-standard-layout class (or array of such types) or reference,
  • has no virtual functions (10.3) and no virtual base classes (10.1),
  • has the same access control (Clause 11) for all non-static data members,
  • has no non-standard-layout base classes,
  • either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and
  • has no base classes of the same type as the first non-static data member. 110

110) This ensures that two subobjects that have the same class type and that belong to the same most derived object are not allocated at the same address (5.10).

这篇关于如何是一类的内存布局与结构体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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