简体   繁体   English

任何人都可以向我解释为什么sizeof函数在下面的代码中返回不同的值?

[英]Can anyone explain to me why the sizeof function returns different values in below code?

Can anyone explain me why the sizeof function returns different values in the code below? 任何人都可以解释为什么sizeof函数在下面的代码中返回不同的值?

//static member
class one
{
  public :
  static const int a = 10;
};
//non static member
class two
{
  public :
  int a;
};
int main()
{
  cout << sizeof(one);       //print 1 to lcd
  cout << sizeof(two);       //print 4 to lcd,differ from size of one class
}

The first thing you should learn is that sizeof is not a function, it's an operator just like + or || 你应该学习的第一件事是sizeof不是一个函数,它是一个像+||的运算符 .

Then as for your question. 至于你的问题。 Static member variables are not actually in the class the same way non-static member variables are, so a class with only static members will have zero size. 静态成员变量实际上不是课堂以同样的方式非静态成员变量,所以只有静态成员类将有大小为零。 But at the same time all objects needs to be addressable, and therefore have, which is why sizeof give you 1 for the first class. 但同时所有对象都需要可寻址,因此具有,这就是sizeof为第一堂课提供1原因。

one has no non-static members, so an instance of it is empty. one没有非静态成员,因此它的一个实例是空的。 The static member is not contained in any object of that type, but exists independently of any objects. 静态成员不包含在该类型的任何对象中,而是独立于任何对象而存在。 It has size 1, rather than zero, because C++ doesn't allow types to have size zero (in order to ensure that different objects have different addresses). 它的大小为1而不是零,因为C ++不允许类型的大小为零(以确保不同的对象具有不同的地址)。

two does have a non-static member, so an instance has to be large enough to contain that member. two确实有一个非静态成员,因此实例必须足够大才能包含该成员。 In your case, its size is 4, the same as the size of its int member. 在您的情况下,其大小为4,与其int成员的大小相同。

Static data members are not stored in the class itself and therefore will not contribute to the sizeof the class. 静态数据成员不存储在类本身中,因此不会对类的sizeof有所贡献。 We can see this by going to the draft C++ standard section 9.4.2 Static data members which says: 我们可以通过转到草案C ++标准部分9.4.2静态数据成员来看到这一点:

A static data member is not part of the subobjects of a class. 静态数据成员不是类的子对象的一部分。

Class one has a size of 1 since complete objects shall have non-zero size, from section 9 Classes which says: one的大小为1 ,因为完整的对象应具有非零大小,从段9 ,其表示:

Complete objects and member subobjects of class type shall have nonzero size. 类类型的完整对象和成员子对象应具有非零大小。 106 106

Note, that sizeof is an operator not a function. 注意, sizeof是一个操作符而不是一个函数。

In one the static variable a would not be considered in the calculation of the size of the class/object. one ,在计算类/对象的大小时不考虑静态变量a

In two , the a would be considered, in this case equivalent to the sizeof(int) . two中, a将被认为是,在这种情况下,等同于sizeof(int)

Notes: 笔记:

  • sizeof is an operator, not a function. sizeof是一个运算符,而不是一个函数。
  • The size of a class may not be 0, hence, the size of one must be something, hence it has a size of 1. 一类的大小可能不是0,因此,大小one必须是这样,因而它的尺寸为1。

Useful reference on the sizeof operator; 有关sizeof运算符的有用参考; http://en.cppreference.com/w/cpp/language/sizeof http://en.cppreference.com/w/cpp/language/sizeof

Note: original question the variable was tow not two . 注:原题的变量是towtwo

Simple answer is that one and tow are different classes with different sizes. 简单的答案是, onetow是不同的类,大小不同。

tow contains int which I assume is 4 bytes on your compiler. tow包含int,我假设你的编译器是4个字节。 I think you understand that part. 我想你明白那一部分。

A static member is not present in every instance of a class, but it is a global variable shared between all classes. 静态成员不存在于类的每个实例中,但它是在所有类之间共享的全局变量。 As such, it is not included in the class size. 因此,它不包括在班级规模中。 This is because sizeof is commonly used to allocate memory for an object, and there is no need to allocate memory for a variable which isn't in the class instance. 这是因为sizeof通常用于为对象分配内存,并且不需要为不在类实例中的变量分配内存。 This is why one isn't 4 bytes. 这就是为什么one不是4个字节。

The reason why it is 1 byte is because the C++ standard doesn't allow a class to have a size of 0 bytes, so the compiler has padded it up to a non-0 size. 它是1字节的原因是因为C ++标准不允许类具有0字节的大小,因此编译器已将其填充为非0大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM