6.3 sizeof(5)
答案:
正确的程序如下:
面试例题8:写出下面sizeof的答案。[德国某著名软件咨询企业2005年面试题]
- #include <iostream>
- #include <string>
- using namespace std;
- int main(int argc,char *argv[])
- {
- string strArr1[] = {"Trend",
- "Micro","Soft"};
- string *pStrArr1 = new string[2];
- pStrArr1[0] = "US";
- pStrArr1[1] = "CN";
- cout << sizeof(strArr1) << endl;
- cout << sizeof(string) << endl;
- for(int i =0; i< sizeof(strArr1)/
- sizeof(string);i++)
- cout << strArr1[i];
- cout << endl;
- for(int j =0; j< sizeof(*pStrArr1)*2/
- sizeof(string);j++)
- cout << pStrArr1[j];
- return 0;
- }
- #include <iostream>
- #include <complex>
- using namespace std;
- class Base
- {
- public:
- Base() { cout<<"Base-ctor"<<endl; }
- ~Base() { cout<<"Base-dtor"<<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-ctor"
- <<endl; }
- ~Derived() { cout<<"Derived-dtor"
- <<endl; }
- void f(complex<double>) { cout
- <<"Derived::f(complex)"<<endl; }
- virtual void g(int i = 20) {cout
- <<"Derived::g()"<<i<<endl; }
- };
- int main()
- {
- Base b;
- Derived d;
- Base* pb = new Derived;
- //从下面的4个答案中选出1个正确的答案
- cout<<sizeof(Base)<<"tt"<<endl;
- //A.4 B.32 C.20
- //D Platform-dependent
- cout<<sizeof(Derived)<<"bb"<<endl;
- //A.4 B.8 C.36 D.Platform-dependent
- }
解析:求类base的大小。因为类base只有一个指针,所以类base的大小是4。Derive大小与base类似,所以也是4。
答案:A,A。
面试例题9:以下代码的输出结果是多少?[中国某著名计算机金融软件公司2006年面试题]
- char var[10]
- int test(char var[])
- {
- return sizeof(var)
- };
A.10 B.9 C.11 D.4
解析:因为var[]等价于*var,已经退化成一个指针了,所以大小是4。
答案:D。