关闭

sizeof 32bit 64bit

1467人阅读 评论(0) 收藏 举报
	printf("short:%d\n",sizeof(short)); 
	printf("double:%d\n",sizeof(double)); 
	printf("long double:%d\n",sizeof(long double)); 
	printf("long:%d\n",sizeof(long)); 
	printf("int:%d\n",sizeof(int)); 
	printf("float:%d\n",sizeof(float)); 
	printf("char:%d\n",sizeof(char)); 





只要是指针,包括char *,sizeof为4。

对数组名求sizeof时,不把它当指针看,sizeof为该数组实际分配的字节数,char[]类型数组也遵循这条规律,数组元素个数可通过初始化表隐形指定。

--------------------------------------------------------------------------------------------------------------------------------------------

以上讨论针对32bit cpu。 

32bit cpu 8字节的类型只有double、 long double和long long。

对于64bit cpu 指针和带一个long的类型变为原来的两倍,其他不变(包括double)。

运行结果如下:(分别用蓝色和红色标记了两者的差异)

——————————————————————————

机器平台:X86_64 处理器

操作系统:Red Hat 4.1.2-14

编译器: gcc version 4.1.2 20070626

Size of char is:                                   1

Size of unsigned char is:                     1

Size of signed char is:                  1

Size of int is:                                    4

Size of short is:                                2

Size of long is:                                  8

Size of long int is:                             8

Size of signed int is:                           4

Size of unsigned int is:                       4

Size of unsigned long int is:                8

Size of long long int is:                       8

Size of unsigned long long is:              8

Size of float is:                    4

Size of double is:                 8

Size of long double is:          16

Size of (void *) is:           8

Size of (char *) is:           8

Size of (int *) is:             8

Size of (long *) is:           8

Size of (long long *) is:   8

Size of (float *) is:          8

Size of (double *) is:              8

——————————————————————————

机器平台: X86_32 处理器

操作系统:  Windows XP SP2

编译器:    VS2005自带的C++编译器

Size of char is:                            1

Size of unsigned char is:              1

Size of signed char is:           1

Size of int is:                       4

Size of short is:                    2

Size of long is:                     4

Size of long int is:                4

Size of signed int is:             4

Size of unsigned int is:          4

Size of unsigned long int is:         4

Size of long long int is:                8

Size of unsigned long long is:       8

Size of float is:                    4

Size of double is:                 8

Size of long double is:          8

Size of (void *) is:          4    

Size of (char *) is:          4

Size of (int *) is:             4

Size of (long *) is:          4

Size of (long long *) is:   4

Size of (float *) is:          4

Size of (double *) is:       4

---------------------------------------------------------------

(1)空结构体或类,sizeof大小为1

(2)c++中静态成员存放在全局数据区,sizeof计算栈中分配大小,故不包含在内

(3)char数组作为sizeof参数时不退化,作为strlen参数时退化为char *

(4)数组作为函数形参时,退化为指针,故其sizeof返回指针长度



1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:45440次
    • 积分:735
    • 等级:
    • 排名:千里之外
    • 原创:26篇
    • 转载:14篇
    • 译文:0篇
    • 评论:2条
    最新评论