面试中你不可回避的C、C++的问题(三)
2014-03-01 16:19
337 查看
本节继续上一次的关于sizeof的讲解:
这次主要是探讨一下,关于sizeof对于类以及对象之间的内存的大小的关系
二维指针域数组的关系
#include <stdio.h>
int main()
{
//存储的是指针所以是3*4*4=48
int ** a[3][4];
printf("%d\n",sizeof(a));//48
char** b[3][4];
printf("%d\n",sizeof(b));//48
return 0;
}空类的大小,以及带有虚函数的空类的大小
#include <stdio.h>
#include <iostream>
using namespace std;
class A
{
};
class B
{
public:
virtual void f() {}
};
int main()
{
A a;
cout << sizeof(a) << endl;//1
B b;
cout << sizeof(b) << endl;//4
return 0;
}再来讨论一下,带有类的成员函数以及继承类对象的大小
#include <stdio.h>
#include <iostream>
#include <complex>
using namespace std;
class Base
{
public:
Base() { cout << "Base Constructor !" << endl; }
~Base() { cout << "Base Destructor !" << endl; }
virtual void f(int ) { cout << "Base::f(int)" << endl; }
virtual void f(double) { cout << "Base::f(double)" << endl; }
virtual void g(int i=10) { cout << "Base::g()" << i << endl; }
void g2(int i=10) { cout << "Base::g2()" << i << endl; }
};
class Derived:public Base
{
public:
Derived() { cout << "Derived Constructor !" << endl; }
~Derived() { cout << "Derived Destructor !" << endl; }
void f(complex<double>) { cout << "Derived::f(complex)" << endl; }
virtual void g(int i=20) { cout << "Derived::g()" << i << endl; }
};
int main()
{
Base c;
Derived d;
Base* pb = new Derived;
cout << sizeof(Base) << endl;//4
cout << sizeof(Derived) << endl;//4
cout << sizeof(pb) << endl;//4
cout << sizeof(c) << endl;//4
cout << sizeof(d) << endl;//4
return 0;
}
这次主要是探讨一下,关于sizeof对于类以及对象之间的内存的大小的关系
二维指针域数组的关系
#include <stdio.h>
int main()
{
//存储的是指针所以是3*4*4=48
int ** a[3][4];
printf("%d\n",sizeof(a));//48
char** b[3][4];
printf("%d\n",sizeof(b));//48
return 0;
}空类的大小,以及带有虚函数的空类的大小
#include <stdio.h>
#include <iostream>
using namespace std;
class A
{
};
class B
{
public:
virtual void f() {}
};
int main()
{
A a;
cout << sizeof(a) << endl;//1
B b;
cout << sizeof(b) << endl;//4
return 0;
}再来讨论一下,带有类的成员函数以及继承类对象的大小
#include <stdio.h>
#include <iostream>
#include <complex>
using namespace std;
class Base
{
public:
Base() { cout << "Base Constructor !" << endl; }
~Base() { cout << "Base Destructor !" << endl; }
virtual void f(int ) { cout << "Base::f(int)" << endl; }
virtual void f(double) { cout << "Base::f(double)" << endl; }
virtual void g(int i=10) { cout << "Base::g()" << i << endl; }
void g2(int i=10) { cout << "Base::g2()" << i << endl; }
};
class Derived:public Base
{
public:
Derived() { cout << "Derived Constructor !" << endl; }
~Derived() { cout << "Derived Destructor !" << endl; }
void f(complex<double>) { cout << "Derived::f(complex)" << endl; }
virtual void g(int i=20) { cout << "Derived::g()" << i << endl; }
};
int main()
{
Base c;
Derived d;
Base* pb = new Derived;
cout << sizeof(Base) << endl;//4
cout << sizeof(Derived) << endl;//4
cout << sizeof(pb) << endl;//4
cout << sizeof(c) << endl;//4
cout << sizeof(d) << endl;//4
return 0;
}
相关文章推荐
- 面试中你不可回避的C、C++的问题(二)
- 面试中你不可回避的C、C++的问题(一)
- 面试中你不可回避的C、C++的问题(四)
- 中面试中你不可回避的C、C++的问题(一)
- C#中C/C++程序员不可不知的问题
- C++面试问题 11_17
- 面试总结5--C++基础问题Part1
- C++面试之GetMemory问题
- C++面试问题汇总
- C++后台开发面试常见问题汇总
- C++面试问题
- C,C++经典问题,及面试笔试题
- C++面试中关于sizeof问题总结
- 面试中的C++常见问题
- 面试c++问题
- 阿里面试问题(C++方向)
- C++面试常错问题摘要
- C++常见面试问题
- C++面试问题统计
- c++一些 面试可能遇到的问题