当前位置:早雪网网络学院编程文档其他语言 → 关于sizeof操作符的理解和总结

关于sizeof操作符的理解和总结

减小字体 增大字体 作者:未知  来源:supcode.com收集整理  发布时间:2005-7-1 14:55:44
t 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:lingjiexyz@hotmail.com
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(

上一页  [1] [2] [3]  下一页

[数据载入中...] [返回上一页] [打 印]