64位对比32位,最主要的区别就是long和指针的字节变成8了;及其size_t等定义类型也为8字节;
还有一些像类会涉及对齐方式不同,所以也不太相同,有机会对64位深入了解后在做详解..
说明点点,下面运行结果是在codeblocks上面的结果,gnu-gcc-编译器;
也有资料说struct类型,是有对其方式的,不是简单的定义的字节相加..
有机会在做学习,或者看到这篇文章的高手点拨下子..
typedef struct myStruct
{
int a;
char*p;
double b;
}MyStruct;
void f3_sizeof()
{
cout << "sizeof(char*)" << sizeof(char*) <<endl; //4
cout << "sizeof(int*)" << sizeof(int*) <<endl;
cout << "sizeof(double*)" << sizeof(double*) <<endl;
cout << "sizeof(char)" << sizeof(char) <<endl; //1
cout <<"sizeof(unsiged int )"<<sizeof(unsigned int )<<endl; //4
cout <<"sizeof(int)" << sizeof(int) <<endl; //4
cout <<"sizeof(long)"<<sizeof(long)<<endl; //4
cout <<"sizeof(double)" << sizeof(double) <<endl; //8
char c1[]="baby!!";
cout <<"sizeof(c1) "<<sizeof(c1)<<endl; //7
cout <<"strlen(c1)"<<strlen(c1)<<endl; //6
char c2[10]="baby!!";
cout <<"sizeof(c2) "<<sizeof(c2)<<endl; //10
cout <<"strlen(c2)"<<strlen(c2)<<endl; //6
string str ="hello";
cout <<"sizeof(str)"<<sizeof(str)<<endl; //4 ???? 指针形式
cout <<"str.size()"<<str.size()<<endl; //5
char *p1=(char *)malloc(100);
memset(p1,0,100); //必须先初始化,才能求出正确的值
memset(p1,1,6);
cout<<"sizeof(p1)"<<sizeof(p1)<<endl; //4
cout <<"strlen(p1)"<<strlen(p1)<<endl; //6
char *pstr ="hello";
cout<<"sizeof(pstr)"<<sizeof(pstr)<<endl; //4
cout <<"strlen(pstr)"<<strlen(pstr)<<endl; //5
char a[10]={1,2};
cout<<"sizeof(a)"<<sizeof(a)<<endl; //10*类型(1)
cout <<"strlen(a)"<<strlen(a)<<endl; //2
char b[10];
memset(b,0,10); //必须初始化,所有位置为空
cout<<"sizeof(b)"<<sizeof(b)<<endl; //10*类型(1)
cout <<"strlen(b)"<<strlen(b)<<endl; //0
cout<<"sizeof(LNode1)"<<sizeof(LNode1)<<endl; //内数据类型相加,不算函数,不算静态局部变量
MyStruct s[10];
MyStruct s1;
MyStruct *ps1=(MyStruct*)malloc(sizeof(MyStruct));
MyStruct *ps2=new MyStruct;
cout<<"sizeof(MyStruct)"<<sizeof(MyStruct)<<endl; //16,类内成员占用内存为最大的的占用的整数倍
cout<<"sizeof(s)"<<sizeof(s)<<endl; //16*10=160
cout<<"sizeof(s1)"<<sizeof(s1)<<endl; //对象,和类占用空间相同
cout<<"sizeof(ps1)"<<sizeof(ps1)<<endl; //指针,4
cout<<"sizeof(ps2)"<<sizeof(ps2)<<endl; //指针,4
}
Data type | Data length(32bit) | Data length(64bit) |
char | 8 (1位) | 8 (1位) |
unsigned char | 8 (1位) | 8 (1位) |
short | 16 (2位) | 16 (2位) |
unsigned short | 16 (2位) | 16 (2位) |
int | 32 (4位) | 32 (4位) |
unsigned int | 32 (4位) | 32 (4位) |
long | 32 (4位) | 64(8位) |
unsigned long | 32 (4位) | 64 (8位) |
long long | 64 | 64(8位) |
point | 32 (4位) | 64(8位) |
size_t | 32 (4位) | 64(8位) |
ssize_t | 32 (4位) | 64(8位) |
off_t | 32 (4位) | 64(8位) |
double | 64(8位) | 64(8位) |
(2011-10-29 21:42:41)