关于sizeof 运算符的总结: 先看一些别人的介绍总结: 1 .From: <<c++ primary 3rd Edition>> siseof 操作符的作用是返回一个对象或类型名的字节长度它有以下三种形式 sizeof (type name ); sizeof ( object ); sizeof object; 返回值的类型是size_t 这是一种与机器相关的typedef 定义我们可以在cstddef 头文 件中找到它的定义下面的例子使用了sizeof 的两种格式 #include <cstddef> int ia[] = { 0, 1, 2 }; // sizeof 返回整个数组的大小 size_t array_size = sizeof ia; // sizeof 返回int 类型的大小 size_t element_size = array_size / sizeof( int ); 当sizeof 操作符应用在数组上时例如上面例子中的ia 它返回整个数组的字节长度 而不是第一个元素的长度也不是ia 包含的元素的个数例如在一台int 类型是4 个字节 长的机器上sizeof 指示ia 的长度是12 字节类似地当我们写如下代码时 int *pi = new int[ 3 ]; size_t pointer_size = sizeof ( pi ); sizeof(pi)返回的值是指向int 型的指针的字节长度而不是pi 指向的数组的长度 下面的小函数可以用来练习sizeof()操作符 #include <string> #include <iostream> #include <cstddef> int main() { size_t ia; ia = sizeof( ia ); // ok ia = sizeof ia; // ok // ia = sizeof int; // 错误 ia = sizeof( int ); // ok int *pi = new int[ 12 ]; cout << "pi: " << sizeof( pi ) << " *pi: " << sizeof( *pi ) << endl; // 一个string 的大小与它所指的字符串的长度无关 string st1( "foobar" ); string st2( "a mighty oak" ); string *ps = &st1; cout << "st1: " << sizeof( st1 ) << " st2: " << sizeof( st2 ) << " ps: " << sizeof( ps ) << " *ps: " << sizeof( *ps ) << endl; cout << "short :\t" << sizeof(short) << endl; cout << "short* :\t" << sizeof(short*) << endl; cout << "short& :\t" << sizeof(short&) << endl; cout << "short[3] :\t" << sizeof(short[3]) << endl; } 编译并运行它产生如下结果 pi: 4 *pi: 4 st1: 12 st2: 12 ps: 4 *ps: 12 short : 2 short* : 4 short& : 2 short[3]: 6 正如上面的例子程序所显示的那样应用在指针类型上的sizeof 操作符返回的是包含该 类型地址所需的内存长度但是应用在引用类型上的sizeof 操作符返回的是包含被引用对 象所需的内存长度 sizeof 操作符应用在char 类型上时在所有的C++实现中结果都是1 // 在所有的实现中保证为1 size_t char_size = sizeof( char ); sizeof 操作符在编译时刻计算因此被看作是常量表达式它可以用在任何需要常量表 达式的地方如数组的维数或模板的非类型参数例如 // ok: 编译时刻常量表达式 int array[ sizeof( some_type_T )]; 2. From 网友.
解析C语言中的sizeof http://www.ccidnet.com 作者:方敏、吴鸣鸣 (2001-04-19 14:43:36) 一、sizeof的概念 sizeof是C语言的一种单目操作符,如C语言的其他操作符++、--等。它并不是函数。sizeof操作符以字节形式给出了其操作数的存储大小。操作数可以是一个表达式或括在括号内的类型名。操作数的存储大小由操作数的类型决定。 二、sizeof的使用方法 1、用于数据类型 sizeof使用形式:sizeof(type) 数据类型必须用括号括住。如sizeof(int)。 2、用于变量 sizeof使用形式:sizeof(var_name)或sizeof var_name 变量名可以不用括号括住。如sizeof (var_name),sizeof var_name等都是正确形式。带括号的用法更普遍,大多数程序员采用这种形式。 注意:sizeof操作符不能用于函数类型,不完全类型或位字段。不完全类型指具有未知存储大小的数据类型,如未知存储大小的数组类型、未知内容的结构或联合类型、void类型等。 如sizeof(max)若此时变量max定义为int max(),sizeof(char_v) 若此时char_v定义为char char_v [MAX]且MAX未知,sizeof(void)都不是正确形式。 三、sizeof的结果 sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型。该类型保证能容纳实现所建立的最大对象的字节大小。 1、若操作数具有类型char、unsigned char或signed char,其结果等于1。 ANSI C正式规定字符类型为1字节。 2、int、unsigned int 、short int、unsigned short 、long int 、unsigned long 、float、double、long double类型的sizeof 在ANSI C中没有具体规定,大小依赖于实现,一般可能分别为2、2、2、2、4、4、4、8、10。 3、当操作数是指针时,sizeof依赖于编译器。例如Microsoft C/C++7.0中,near类指针字节数为2,far、huge类指针字节数为4。一般Unix的指针字节数为4。 4、当操作数具有数组类型时,其结果是数组的总字节数。 5、联合类型操作数的sizeof是其最大字节成员的字节数。结构类型操作数的sizeof是这种类型对象的总字节数,包括任何垫补在内。 让我们看如下结构: struct {char b; double x;} a; 在某些机器上sizeof(a)=12,而一般sizeof(char)+ sizeof(double)=9。 这是因为编译器在考虑对齐问题时,在结构中插入空位以控制各成员对象的地址对齐。如double类型的结构成员x要放在被4整除的地址。 6、如果操作数是函数中的数组形参或函数类型的形参,sizeof给出其指针的大小。 四、sizeof与其他操作符的关系 sizeof的优先级为2级,比/、%等3级运算符优先级高。它可以与其他操作符一起组成表达式。如i*sizeof(int);其中i为int类型变量。 五、sizeof的主要用途 1、sizeof操作符的一个主要用途是与存储分配和I/O系统那样的例程进行通信。例如: void *malloc(size_t size), size_t fread(void * ptr,size_t size,size_t nmemb,FILE * stream)。 2、sizeof的另一个的主要用途是计算数组中元素的个数。例如: void * memset(void * s,int c,sizeof(s))。 六、建议 由于操作数的字节数在实现时可能出现变化,建议在涉及到操作数字节大小时用ziseof来代替常量计算。 (http://www.fanqiang.com) 3. From smth c++版 发信人: jiangfei (afei), 信区: CPlusPlus 标 题: 关于sizeof的小问题,谢谢赐教 发信站: BBS 水木清华站 (Tue Mar 15 15:08:06 2005), 站内 #include<iostream> using namespace std;
int main(){ char str[] = "a short string"; string st = "a short string"; cout << sizeof (str) //15,算'\0' << endl << sizeof (st) //16,why? << endl; if(str[14] == '\0') cout << "ok1" << endl;//ok1.I c. if( st[15] == '\0') cout << "ok2" << endl;//ok2,why?
} -- ※ 来源:·BBS 水木清华站 http://smth.org·[FROM: 202.118.229.*] [本篇全文] [回复文章] [回信给作者] [本篇作者:xxxdg] [进入讨论区] [返回顶部] 2 发信人: xxxdg (沙漠蜥蜴), 信区: CPlusPlus 标 题: Re: 关于sizeof的小问题,谢谢赐教 发信站: BBS 水木清华站 (Tue Mar 15 15:15:18 2005), 站内 你要不要试试 string st="hello"; cout<<sizeof(st)<<endl; 呵呵 【 在 jiangfei (afei) 的大作中提到: 】 : #include<iostream> : using namespace std; : int main(){ : char str[] = "a short string"; : string st = "a short string"; : cout << sizeof (str) //15,算'\0' : << endl : << sizeof (st) //16,why? : << endl; : if(str[14] == '\0') : cout << "ok1" << endl;//ok1.I c. : if( st[15] == '\0') : cout << "ok2" << endl;//ok2,why? : } --
※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.77.*] [本篇全文] [回复文章] [回信给作者] [本篇作者:jiangfei] [进入讨论区] [返回顶部] 3 发信人: jiangfei (afei), 信区: CPlusPlus 标 题: Re: 关于sizeof的小问题,谢谢赐教 发信站: BBS 水木清华站 (Tue Mar 15 15:24:54 2005), 站内 blush! 见笑! but why ??? all are 16? 【 在 xxxdg (沙漠蜥蜴) 的大作中提到: 】 : 你要不要试试 : string st="hello"; : cout<<sizeof(st)<<endl; : ................... -- ※ 修改:·jiangfei 於 Mar 15 15:27:10 2005 修改本文·[FROM: 202.118.229.*] ※ 来源:·BBS 水木清华站 http://smth.org·[FROM: 202.118.229.*] [本篇全文] [回复文章] [回信给作者] [本篇作者:jiangfei] [进入讨论区] [返回顶部] 4 发信人: jiangfei (afei), 信区: CPlusPlus 标 题: Re: 关于sizeof的小问题,谢谢赐教 发信站: BBS 水木清华站 (Tue Mar 15 15:30:53 2005), 站内 The sizeof operator cannot return the size of dynamically allocated arrays or external arrays. But return 16 for sizeof(string) stands for nothing at all? 【 在 jiangfei (afei) 的大作中提到: 】 : blush! : 见笑! : but why ??? : ................... -- ※ 来源:·BBS 水木清华站 http://smth.org·[FROM: 202.118.229.*] [本篇全文] [回复文章] [回信给作者] [本篇作者:xxxdg] [进入讨论区] [返回顶部] 5 发信人: xxxdg (沙漠蜥蜴), 信区: CPlusPlus 标 题: Re: 关于sizeof的小问题,谢谢赐教 发信站: BBS 水木清华站 (Tue Mar 15 15:33:44 2005), 站内 think and answer my question , then you will got it class a { public: a(){ _data = new int [100];} ~a(){ delete [] _data;} int * _data; }; a _x; cout<<sizeof(_x)<<endl; output choice: 1) 100 2) 128 3) 4 4) 4 + sizeof(a and ~a) 【 在 jiangfei (afei) 的大作中提到: ;】 : blush! : 见笑! : but why ??? : all are 16?
--
※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.77.*] [本篇全文] [回复文章] [回信给作者] [本篇作者:jiangfei] [进入讨论区] [返回顶部] 6 发信人: jiangfei (afei), 信区: CPlusPlus 标 题: Re: 关于sizeof的小问题,谢谢赐教 发信站: BBS 水木清华站 (Tue Mar 15 15:36:25 2005), 站内 但为什么 string st = "a short string"; st[15] == '\0'?? 【 在 xxxdg (沙漠蜥蜴) 的大作中提到: 】 : 你要不要试试 : string st="hello"; : cout<<sizeof(st)<<endl; : ................... -- ※ 来源:·BBS 水木清华站 http://smth.org·[FROM: 202.118.229.*] [本篇全文] [回复文章] [回信给作者] [本篇作者:xxxdg] [进入讨论区] [返回顶部] 7 发信人: xxxdg (沙漠蜥蜴), 信区: CPlusPlus 标 题: Re: 关于sizeof的小问题,谢谢赐教 发信站: BBS 水木清华站 (Tue Mar 15 15:37:40 2005), 站内 can operator [] be overloaded? 【 在 jiangfei (afei) 的大作中提到: 】 : 但为什么 : string st = "a short string"; : st[15] == '\0'?? --
※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.77.*] [本篇全文] [回复文章] [回信给作者] [本篇作者:owl2008] [进入讨论区] [返回顶部] 8 发信人: owl2008 (owl), 信区: CPlusPlus 标 题: Re: 关于sizeof的小问题,谢谢赐教 发信站: BBS 水木清华站 (Tue Mar 15 15:38:27 2005), 站内 type object; sizeof object == sizeof type 【 在 jiangfei (afei) 的大作中提到: 】 : The sizeof operator cannot return the size of dynamically allocated arrays or external arrays. : But return 16 for sizeof(string) stands for nothing at all? -- ID owl MSN:[email protected] QQ:85475939 BLOG:http://blog.csdn.net/owl2008/
※ 来源:·BBS 水木清华站 smth.org·[FROM: 220.184.16.*]
接下来 ,总结一下我自己的理解: <1> . 对齐问题很微妙: <2> . 一个重要用途: 由于操作数的字节数在实现时可能出现变化。 建议在涉及到操作数字节大小时用ziseof来代替常量计算。 int ia[] = { 0, 1, 2 }; // sizeof 返回整个数组的大小 size_t array_size = sizeof ia; // sizeof 返回int 类型的大小 size_t element_size = array_size / sizeof( int ); <3> . 当操作数是指针时,sizeof依赖于编译器,是指针大小而非所指元素大小. 在现有32位机器,VC6.0环境中,sizeof(pointer) 为 4。 <4> . 当操作数具有数组类型时,其结果是数组的总字节数。 <5> . 一个string 的大小(sizeof)与它所指的字符串的长度无关. sizeof(string str="hello");sizeof(string str="a long long long long long string") 在现有环境(32位机器,VC6.0编译器)都是16。 <6> . 类的sizeof由于对齐等原因变得很微妙,尤其在具有虚函数时更加难以捉摸. 比如, class a { public: a(){ _data = new char [100];} ~a(){ delete [] _data;} char * _data; //sizeof(pointer) = 4 long data2; //sizeof(long) = 4 char dat[10]; //sizeof(dat) = 10 char dat1; //sizeof(char) = 1 }; a _x; cout<<sizeof(_x)<<endl;//输出20 变为: class a { public: a(){ _data = new char [100];} ~a(){ delete [] _data;} char * _data; //sizeof(pointer) = 4 char dat[10]; //sizeof(dat) = 10 换位置 long data2; //sizeof(long) = 4 换位置 char dat1; //sizeof(char) = 1 }; a _x; cout<<sizeof(_x)<<endl;//输出24 
|