您的位置:首页 > 编程语言 > C语言/C++

面试常考的C/C++知识点sizeof

2012-04-18 19:31 162 查看
#include <iostream>
using namespace std;
struct{
short a;//short占2个字节
short b;
short c;
}A;

class B{
private:
bool a;//1
int b;//4
bool c;//1
};

class C{
private:
int a;//4
bool b;//1
bool c;//1
};

class D{

};

int main(){
int a = 10;
int b[100] = {1,2,3};
char c[20];
int **d[3][4];
int *ptr = &a;
char *cptr = "good job";
cout << "sizeof(b) " << sizeof(b) << endl
<< "sizeof(c) "<< sizeof(c) << endl
<< "sizeof(d) " << sizeof(d) << endl

<< "sizeof(ptr) " << sizeof(ptr) << endl
<< "sizeof(cptr) " <<  sizeof(cptr)	<< endl

<< "sizeof(A) " << sizeof(A) << endl
<< "sizeof(B) " << sizeof(B) << endl
<< "sizeof(C) " << sizeof(C) << endl
<< "sizeof(D) " << sizeof(D) << endl;
}

运行结果:

sizeof(b) 400

sizeof(c) 20

sizeof(d) 48

sizeof(ptr) 4

sizeof(cptr) 4

sizeof(A) 6

sizeof(B) 12

sizeof(C) 8

sizeof(D) 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: