[英]Size of class with int and size of class with int inside a function
i am learning about C++ and object oriented programming. 我正在学习C ++和面向对象的编程。 Have a small doubt -
有一个小疑问-
class CA
{
void fun()
{
int x;
}
};
class CB
{
int y;
};
int main()
{
CA obj;
CB obj1;
cout<<sizeof(obj)<<'\t'<<sizeof(obj1);
}
When i run the above code, i get size of obj as '1' byte while size of obj1 as '4' byte. 当我运行上面的代码时,我得到的obj的大小为“ 1”字节,而obj1的大小为“ 4”字节。 Why is that?
这是为什么? Both the classes have integers, so size should be the same?
两个类都有整数,因此大小应该相同吗? How is the size of a class is calculated?
班级人数是如何计算的?
CA
doesn't have any member variables. CA
没有任何成员变量。 (A non- virtual
function such as fun
and anything it might contain does not contribute to the size of an instance of a class.) But sizeof(CA)
can't be zero because otherwise pointer arithmetic would break horribly on an array of CA
objects! (一个非
virtual
函数,例如fun
及其可能包含的任何东西,都不会增加类实例的大小。)但是sizeof(CA)
不能为零,因为否则,指针算法会在CA
数组上严重破坏对象! So your compiler picks a minimum value. 因此,您的编译器会选择一个最小值。 1 is allowed since that's no smaller than
sizeof(char)
. 允许1,因为它不小于
sizeof(char)
。
The size of CB
is bounded by the size of an int
; CB
的大小由int
的大小限制; it could be greater than that due to padding . 由于填充 ,它可能比更大。
The variable x
inside the function 函数内部的变量
x
void fun()
{
int x;
}
is not a data member of the class. 不是该类的数据成员。 It is a local variable of the function that is not alive outside the function.
它是该函数的局部变量,在该函数外部不存在。 The class does not have data members.
该类没有数据成员。
The size of a complete object (or class) can not be equal to 0. So the compiler sets it to 1. 完整对象(或类)的大小不能等于0。因此,编译器将其设置为1。
In the second case the variable y
is a data member of the class 在第二种情况下,变量
y
是该类的数据成员
class CB
{
int y;
};
Pay attention to that if in the second case you will declare a static data member instead of the non-static member like 请注意,如果在第二种情况下,您将声明一个静态数据成员而不是非静态成员,例如
class CB
{
static int y;
};
then the static member does not constitute the size of the class or an object of the class. 则静态成员并不构成类或类的对象的大小。 Static data members are defined (usually) outside classes.
静态数据成员通常在类外部定义。 Within the class there is obly a declaration of the static member not its definition.
在类中,有一个静态成员的声明,而不是其定义。 SO again the program will output
1
for an object of such a class. 同样,程序将为此类对象输出
1
。
You can use CB to store a value. 您可以使用CB来存储值。 That's why the objects of this class have some extra size.
这就是此类的对象具有一些额外大小的原因。
CB b;
b.y = 42;
You cannot use CA to store a value. 您不能使用CA来存储值。
CA a;
a ... // what would you put there ?
When CA is used, the x
value will live on the stack, outside the object, only while the function is called. 使用CA时,
x
值仅在调用函数时才会存在于对象外部的堆栈中。 As such it doesn't contribute to the object's size. 因此,它不会影响对象的大小。
class CB
{
int y;
};
CB has a non-static data member (non-static members are used for calculating the size of an object among other factors) and hence it has been 4 bytes according to a 32-bit integer. CB具有非静态数据成员 (在其他因素中,非静态成员用于计算对象的大小),因此根据32位整数,它为4字节 。
class CA
{
void fun()
{
int x;
}
};
On the other hand, CA has a non-virtual member function containing a non-static member in it, memory to which memory is only allocated at the time it is called and doesn't contribute to the size of the object. 另一方面,CA具有一个非虚拟成员函数,其中包含一个非静态成员,该内存仅在调用时才分配给该内存,而不会增加对象的大小。 It is treated as an empty class by the compiler.
编译器将其视为空类。 The 1 Byte is given by the compiler to uniquely identify objects by assigning them an address.
1字节由编译器提供,用于通过为对象分配地址来唯一标识对象。
Regards 问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.