简体   繁体   中英

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?

//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 || .

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.

one has no non-static members, so an instance of it is empty. 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).

two does have a non-static member, so an instance has to be large enough to contain that member. In your case, its size is 4, the same as the size of its int member.

Static data members are not stored in the class itself and therefore will not contribute to the sizeof the class. We can see this by going to the draft C++ standard section 9.4.2 Static data members which says:

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:

Complete objects and member subobjects of class type shall have nonzero size. 106

Note, that sizeof is an operator not a function.

In one the static variable a would not be considered in the calculation of the size of the class/object.

In two , the a would be considered, in this case equivalent to the sizeof(int) .

Notes:

  • sizeof is an operator, not a function.
  • The size of a class may not be 0, hence, the size of one must be something, hence it has a size of 1.

Useful reference on the sizeof operator; http://en.cppreference.com/w/cpp/language/sizeof

Note: original question the variable was tow not two .

Simple answer is that one and tow are different classes with different sizes.

tow contains int which I assume is 4 bytes on your compiler. 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. This is why one isn't 4 bytes.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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