一天中的好时光!
我写了一些代码,但我无法理解一些奇怪的内存异常。任何对课堂记忆有适当了解的人都可以给我一个解释吗?
我的代码:
#include <iostream>
using namespace std;
class O
{
O();
~O();
};
class A
{
public:
A();
~A();
void someFunc();
private:
int m_a;
};
class B: public A
{
public:
B();
virtual ~B();
private:
int m_b;
};
class C: public B
{
public:
C();
~C();
private:
char m_c;
};
int main()
{
cout << sizeof(char) << endl;
cout << sizeof(int) << endl;
cout << sizeof(O) << endl;
cout << sizeof(A) << endl;
cout << sizeof(B) << endl;
cout << sizeof(C) << endl;
cin.get();
return 0;
}
输出:
1 //normal for char
4 //normal for int on x32
1 //why empty class occupies 1 byte?
4 //int m_a. Where is 1 byte?
12 //4B virtual function, 8B - m_a and m_b.
16 //char needs 1 byte. Why it gets 3 more?
谢谢关注和解答)