sizeof题
1、在windows下,写出运行结果,每空2分,共10分。
char str[ ]= "hello";
char *p=str;
int n=10;
sizeof(str)=( )
sizeof(p)=( )
sizeof(n)=( )
void func(char str[100])
{ }
sizeof(str)=( )
[解决办法]
- C/C++ code
//数组做参数传递 退化为指针////////////////////关于sizeof计算////////////////////struct { short a1; short a2; short a3;}A;struct { long a1; short a2;}B;class STU{ ;};class _STU{public: int m; int n; int a;private: int x; int y; int z;};class NNN{public: _STU a;//24 STU b; //28private:protected:};class MMM{public: char a,b;};class WWW{public: WWW(); ~WWW();};class HHH{public: HHH(); ~HHH(); char s[100];};class _HHH{public: _HHH(); ~_HHH();private: int a[2];//此处8 char s;//此处4 long d;//此处4 char* p;//此处4};char v[10];int test(char v[]){ return sizeof(v);}class GGG{public: char p; int dd[3]; float e;};int main(void){ char* ss1 = "0123456789" ; char ss2[] ="0123456789" ; char ss3[100] = "0123456789" ; int ss4[100] ; char q1[] = "abc" ; char q2[] = "a\n" ; char* q3 = "a\n" ; char* str1 = (char*)malloc(100); void* str2 = (void*)malloc(100); cout <<sizeof(ss1)<<endl ;//4 cout <<sizeof(ss2)<<endl ;//11 cout <<sizeof(ss3)<<endl ;//100 cout <<sizeof(ss4)<<endl ;//400 cout <<sizeof(q1)<<endl ;//4 cout <<sizeof(q2)<<endl ;//3 cout <<sizeof(q3)<<endl ;//4 cout <<sizeof(A)<<endl ;//6 cout <<sizeof(B)<<endl ;//8 //结构体内存对其 cout <<sizeof(str1)<<endl ;//4 cout <<sizeof(str2)<<endl ;//4 cout <<sizeof(STU)<<endl ;//1 cout <<sizeof(_STU)<<endl ;//24 cout <<sizeof(NNN)<<endl ;//28 cout <<sizeof(MMM)<<endl ;//2 cout <<sizeof(WWW)<<endl ;//1 cout <<sizeof(HHH)<<endl ;//100 cout <<sizeof(_HHH)<<endl ;//20 //内存对其原则 按照4的倍数 cout <<test("1")<<endl ;//4 cout <<sizeof(GGG)<<endl ;//20 //内存对其原则 按照4的倍数 return 0;}//2class AA{private:public:protected:};class BB:public AA{public: int m_x;};int main(void){ BB b1; cout << sizeof(b1) <<endl; //4 return 0;}class AA{public: static int i;};int main(void){ AA a; cout << sizeof(a) << endl; //1 cout << sizeof(a.i) << endl; //4 return 0;}
[解决办法]
编译器生成32位Windows程序:6 4 4 6(最后一个,如果是在函数里面,那么就是4)