📄 subject_40928.htm
字号:
<p>
序号:40928 发表者:常山赵子龙 发表日期:2003-05-22 14:43:32
<br>主题:关于Tc++ 3.0中整型和双精型的存储!
<br>内容:一直没太搞清不同类型的数据在系统内是如何存储的!今天在看〈C++ Primer Plus(第四版)中文版〉时无意中看到了一个例题:<BR><BR>#include <iostream.h><BR><BR>void main()<BR>{<BR> int donuts=6;<BR> double cups=4.5;<BR><BR> cout<<"donuts value="<<donuts;<BR> cout<<" and donuts address="<<&donuts<<"\n";<BR><BR> cout<<"cups value="<<cups;<BR> cout<<" and cups address="<<&cups<<"\n";<BR>}<BR><BR>程序编译无错误,运行结果如下:<BR>donuts value=6 and donuts address=0x1c33fff4<BR>cups value=4.5 and cups address=0x1c33ffec<BR><BR>书上说一般这样的连续赋值都会连续存放的,但两者差8个字节,这说明什么?整型长度为8字节?可我用之前用sizeof(int)测试时刚刚是2啊!? <BR><BR>书上又说每种系统对这两种数据的存储顺序是不一样的,我又想试一下,最把程序的赋值和输出都上下调了一下,内容不变。<BR><BR>#include <iostream.h><BR><BR>void main()<BR>{<BR><BR> double cups=4.5;<BR> int donuts=6;<BR><BR> cout<<"cups value="<<cups;<BR> cout<<" and cups address="<<&cups<<"\n";<BR><BR> cout<<"donuts value="<<donuts;<BR> cout<<" and donuts address="<<&donuts<<"\n";<BR>}<BR><BR>输出:<BR>cups value=4.5 and cups address=0x1c33ffee<BR>donuts value=6 and donuts address=0x1c33ffec<BR><BR>结果又是出人意料:整型仍然被存在了前面,但这回相差倒真是二个字节了(整型字符的长度),但如果这个是正确的,上一个是怎么回呢?<BR><BR>上述二程序都通过编译连接无错误!!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:罗兹维尔 回复日期:2003-05-22 15:45:42
<br>内容:#include <iostream.h><BR><BR>void main()<BR>{<BR> int donuts=6;<BR> double cups=4.5;<BR><BR> cout<<"donuts value="<<donuts;<BR> cout<<" and donuts address="<<&donuts<<"\n";<BR><BR> cout<<"cups value="<<cups;<BR> cout<<" and cups address="<<&cups<<"\n";<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:罗兹维尔 回复日期:2003-05-22 15:46:59
<br>内容:#include <iostream.h><BR><BR>void main()<BR>{<BR><BR> double cups=4.5;<BR> int donuts=6;<BR><BR> cout<<"cups value="<<cups;<BR> cout<<" and cups address="<<&cups<<"\n";<BR><BR> cout<<"donuts value="<<donuts;<BR> cout<<" and donuts address="<<&donuts<<"\n";<BR>}<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:罗兹维尔 回复日期:2003-05-22 15:58:23
<br>内容:donuts value=6 and donuts address=0x1c33fff4<BR>cups value=4.5 and cups address=0x1c33ffec<BR><BR>double cups 放在了 int donuts 的前面阿<BR>说明double是8 bytes
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:常山赵子龙 回复日期:2003-05-23 08:32:50
<br>内容:哈哈,E是在F前面啊,呵呵!<BR>惭愧惭愧!!!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -