﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-no_rain-文章分类-C/C++</title><link>http://www.cppblog.com/nxm1990/category/18157.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 30 Nov 2011 08:26:18 GMT</lastBuildDate><pubDate>Wed, 30 Nov 2011 08:26:18 GMT</pubDate><ttl>60</ttl><item><title>网络编程的小实验（一）</title><link>http://www.cppblog.com/nxm1990/articles/161092.html</link><dc:creator>is-programmer</dc:creator><author>is-programmer</author><pubDate>Mon, 28 Nov 2011 15:17:00 GMT</pubDate><guid>http://www.cppblog.com/nxm1990/articles/161092.html</guid><wfw:comment>http://www.cppblog.com/nxm1990/comments/161092.html</wfw:comment><comments>http://www.cppblog.com/nxm1990/articles/161092.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/nxm1990/comments/commentRss/161092.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/nxm1990/services/trackbacks/161092.html</trackback:ping><description><![CDATA[在没有链接的情况下调用getpeername<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">stdio.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br />#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">stdlib.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br />#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #0000FF; ">string</span><span style="color: #000000; ">.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br />#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">unistd.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br />#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">sys</span><span style="color: #000000; ">/</span><span style="color: #000000; ">socket.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br />#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">netinet</span><span style="color: #000000; ">/</span><span style="color: #0000FF; ">in</span><span style="color: #000000; ">.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br /><br /></span><span style="color: #0000FF; ">#define</span><span style="color: #000000; ">&nbsp;MAXLINE&nbsp;80</span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">#define</span><span style="color: #000000; ">&nbsp;SERV_PORT&nbsp;8000</span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;main(){<br />&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;servsock;<br />&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">((servsock&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;socket(AF_INET,SOCK_STREAM,</span><span style="color: #000000; ">0</span><span style="color: #000000; ">))&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">){<br />&nbsp;&nbsp;&nbsp;&nbsp;perror(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">socket</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #000000; ">1</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;</span><span style="color: #0000FF; ">struct</span><span style="color: #000000; ">&nbsp;sockaddr_in&nbsp;peer;<br />&nbsp;&nbsp;memset(</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">peer,</span><span style="color: #000000; ">0</span><span style="color: #000000; ">,</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(peer));<br />&nbsp;&nbsp;socklen_t&nbsp;peerlen&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(peer);<br />&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;ret;<br />&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">((ret&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;getpeername(servsock,(</span><span style="color: #0000FF; ">struct</span><span style="color: #000000; ">&nbsp;sockaddr</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">peer,</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">peerlen))&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">){<br />&nbsp;&nbsp;&nbsp;&nbsp;perror(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">getpeername</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #000000; ">1</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;meg[</span><span style="color: #000000; ">100</span><span style="color: #000000; ">];<br />&nbsp;&nbsp;inet_ntop(AF_INET,</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">peer,meg,peerlen);<br />&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%s\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,meg);<br />}<br /></span></div>结果：<span class="Apple-style-span" style="font-size: 13px; background-color: #eeeeee; "><span style="color: #000000; ">getpeername:&nbsp;Transport&nbsp;endpoint&nbsp;</span><span style="color: #0000FF; ">is</span><span style="color: #000000; ">&nbsp;not&nbsp;connected</span></span><img src ="http://www.cppblog.com/nxm1990/aggbug/161092.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/nxm1990/" target="_blank">is-programmer</a> 2011-11-28 23:17 <a href="http://www.cppblog.com/nxm1990/articles/161092.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于C中的文件描述符关联C++ 的 iostream</title><link>http://www.cppblog.com/nxm1990/articles/160548.html</link><dc:creator>is-programmer</dc:creator><author>is-programmer</author><pubDate>Sun, 20 Nov 2011 04:55:00 GMT</pubDate><guid>http://www.cppblog.com/nxm1990/articles/160548.html</guid><wfw:comment>http://www.cppblog.com/nxm1990/comments/160548.html</wfw:comment><comments>http://www.cppblog.com/nxm1990/articles/160548.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/nxm1990/comments/commentRss/160548.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/nxm1990/services/trackbacks/160548.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="color: #909090; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #ffffff; "><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Recently I has read object-oriend multithreading using C++ and find a interesting question ,: is it possible for us to use the file descriptor and socket to be linked to the object iostream ? the answer is no... even in the book is yes ,(osolete book);</p></span><span class="Apple-style-span" style="color: #909090; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #ffffff; ">This is a frequent question, and the answer is probably "no". At least with respect to standard C++, there's no way to obtain a descriptor from an fstream object or bind an fstream object to a descriptor. The reason, as your book suggested, is implementation dependecies. Just as new doesn't necessarily have to use malloc() to allocate raw memory, the fstream library isn't required to use stdio.h as its underlying implementation.</span><span class="Apple-style-span" style="color: #909090; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #ffffff; "><p style="line-height: 25px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p></span><img src ="http://www.cppblog.com/nxm1990/aggbug/160548.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/nxm1990/" target="_blank">is-programmer</a> 2011-11-20 12:55 <a href="http://www.cppblog.com/nxm1990/articles/160548.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C中的side effect 和 sequence point </title><link>http://www.cppblog.com/nxm1990/articles/160085.html</link><dc:creator>is-programmer</dc:creator><author>is-programmer</author><pubDate>Mon, 14 Nov 2011 07:08:00 GMT</pubDate><guid>http://www.cppblog.com/nxm1990/articles/160085.html</guid><wfw:comment>http://www.cppblog.com/nxm1990/comments/160085.html</wfw:comment><comments>http://www.cppblog.com/nxm1990/articles/160085.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/nxm1990/comments/commentRss/160085.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/nxm1990/services/trackbacks/160085.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt; ">wiki 中 sequence point 定义：</span><br /><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; ">A&nbsp;</span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; "><strong>sequence point</strong>&nbsp;in&nbsp;<a href="http://en.wikipedia.org/wiki/Imperative_programming" title="Imperative programming" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">imperative programming</a>&nbsp;defines any point in a&nbsp;<a href="http://en.wikipedia.org/wiki/Computer_program" title="Computer program" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">computer program</a>'s&nbsp;<a href="http://en.wikipedia.org/wiki/Execution_(computers)" title="Execution (computers)" class="mw-redirect" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">execution</a>&nbsp;at which it is guaranteed that all<a href="http://en.wikipedia.org/wiki/Side_effect_(computer_science)" title="Side effect (computer science)" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">side effects</a>&nbsp;of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to&nbsp;<a href="http://en.wikipedia.org/wiki/C_(programming_language)" title="C (programming language)" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">C</a>&nbsp;and&nbsp;<a href="http://en.wikipedia.org/wiki/C%2B%2B" title="C++" style="text-decoration: none; color: #0645ad; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; ">C++</a>, because the result of some expressions can depend on the order of evaluation of their subexpressions. Adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation.<br />简单的说就是在C/C++中有一些执行顺序问题，C/C++定义了一些sequence point，在下一个sequence point 执行之前必须把前一个执行完，但是问题是在一个sequence point 和另一个之间还有一些side effect。由于这些就引出一些有趣的问题。<div style="display: inline-block; "></div></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">条件运算符?:、逗号运算符、逻辑与&amp;&amp;、逻辑或||的每一个操作数求值之后是Sequence Point。<br /></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; ">问题一：<br /></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; "><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">f(g(&nbsp;),&nbsp;h(&nbsp;)&nbsp;);</span></div></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; ">先执行谁？答：g( ),h( ) 的顺序不确定，它们执行之后才执行f ( );<br /></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; ">问题二：<br /></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; "><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;i&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;i&nbsp;</span><span style="color: #000000; ">++</span><span style="color: #000000; ">;</span></div></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; ">结果是？ 答：undefined，因为我们知道i++ 是先返回再改变i的值的，那么赋值语句右边就有了值了，那么就存在这样的两种情况：1,i先赋值,再++，那么i现在为2；2，先++，然后赋值，注意此时赋值的内容是还没++的值，所以i为1；根据不同的编译器会得出不同的结果的。<br /></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; ">问题三：<br /></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; "><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;a&nbsp;&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;&nbsp;a&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;(</span><span style="color: #000000; ">++</span><span style="color: #000000; ">a)</span><span style="color: #000000; ">+</span><span style="color: #000000; ">(</span><span style="color: #000000; ">++</span><span style="color: #000000; ">a)</span><span style="color: #000000; ">+</span><span style="color: #000000; ">(</span><span style="color: #000000; ">++</span><span style="color: #000000; ">a);</span></div></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; ">结果是？答：undefined,这个的问题是，到底是先把每个++执行完之后才进行加法还是先把前两个先加再执行最后一个++？两者的答案分别是9，11。当然还有其他情况。<br /></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; ">问题四：<br /></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; "><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;a[</span><span style="color: #000000; ">10</span><span style="color: #000000; ">]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">{</span><span style="color: #000000; ">0</span><span style="color: #000000; ">};&nbsp;a[i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">]&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;i;</span></div></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; ">答：同问题2.<br /></span><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; background-color: #ffffff; font-size: 10pt; ">所以我们要坚持的原则是：<div style="display: inline-block; "></div></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: medium; "><span class="emphasis" style="font-size: 10pt; "><em style="font-weight: bold; font-style: normal; ">在两个Sequence Point之间，同一个变量的值只允许被改变一次</em></span></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">。</span><span class="Apple-style-span" style="font-family: sans-serif; font-size: 13px; line-height: 19px; background-color: #ffffff; "><br /><br /></span><span class="Apple-style-span" style="font-family: sans-serif; font-size: 13px; line-height: 19px; background-color: #ffffff; "><br /><br /><br /><br /></span><img src ="http://www.cppblog.com/nxm1990/aggbug/160085.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/nxm1990/" target="_blank">is-programmer</a> 2011-11-14 15:08 <a href="http://www.cppblog.com/nxm1990/articles/160085.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C中的Comma Operator和typedef，sizeof</title><link>http://www.cppblog.com/nxm1990/articles/160083.html</link><dc:creator>is-programmer</dc:creator><author>is-programmer</author><pubDate>Mon, 14 Nov 2011 06:12:00 GMT</pubDate><guid>http://www.cppblog.com/nxm1990/articles/160083.html</guid><wfw:comment>http://www.cppblog.com/nxm1990/comments/160083.html</wfw:comment><comments>http://www.cppblog.com/nxm1990/articles/160083.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/nxm1990/comments/commentRss/160083.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/nxm1990/services/trackbacks/160083.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; font-size: 10pt; background-color: #ffffff; ">逗号运算符（Comma Operator）<a id="id2762605" class="indexterm" style="color: blue; text-decoration: underline; "></a></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">也是一种双目运算符，它的形式是<code class="literal"><span style="font-size: 10pt; ">表达式1, 表达式2</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">，两个表达式不要求类型一致，左边的表达式1先求值，求完了直接把值丢掉，再求右边表达式2的值作为整个表达式的值。<div style="display: inline-block; "></div></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">注意，函数调用时各实参之间也是用逗号隔开，这种逗号是分隔符而不是逗号运算符。但可以这样使用逗号运算符：<br /><span class="Apple-style-span" style="font-family: 'courier new', courier, fixed; white-space: pre; background-color: #f0f0f0; font-size: 10pt; "><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">f(a,&nbsp;(t</span><span style="color: #000000; ">=</span><span style="color: #000000; ">3</span><span style="color: #000000; ">,&nbsp;t</span><span style="color: #000000; ">+</span><span style="color: #000000; ">2</span><span style="color: #000000; ">),&nbsp;c)</span></div></span></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: medium; "><code class="literal"><span style="font-size: 10pt; ">sizeof</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">是一个很特殊的运算符，它有两种形式：&#8220;<span class="quote" style="font-size: 10pt; ">sizeof 表达式</span></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">&#8221;和&#8220;<span class="quote" style="font-size: 10pt; ">sizeof(类型名)</span></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">&#8221;。这个运算符很特殊，&#8220;<span class="quote" style="font-size: 10pt; ">sizeof 表达式</span></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">&#8221;中的子表达式并不求值，而只是根据类型转换规则求得子表达式的类型，然后把这种类型所占的字节数作为整个表达式的值。有些人喜欢写成&#8220;<span class="quote" style="font-size: 10pt; ">sizeof(表达式)</span></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">&#8221;的形式也可以。<br /></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: medium; "><code class="literal"><span style="font-size: 10pt; ">sizeof</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">运算符的结果是<code class="literal"><span style="font-size: 10pt; ">size_t</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">类型的，这个类型定义在<code class="literal"><span style="font-size: 10pt; ">stddef.h</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">头文件中，不过你的代码中只要不出现<code class="literal"><span style="font-size: 10pt; ">size_t</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">这个类型名就不用包含这个头文件，比如像上面的例子就不用包含这个头文件。C标准规定<code class="literal"><span style="font-size: 10pt; ">size_t</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">是一种无符号整型，编译器可以用<code class="literal"><span style="font-size: 10pt; ">typedef</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">做一个类型声明，<div style="display: inline-block; "></div></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">那么<code class="literal"><span style="font-size: 10pt; ">size_t</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">就代表<code class="literal"><span style="font-size: 10pt; ">unsigned long</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">型。不同平台的编译器可能会根据自己平台的具体情况定义<code class="literal"><span style="font-size: 10pt; ">size_t</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">所代表的类型，比如有的平台定义为<code class="literal"><span style="font-size: 10pt; ">unsigned long</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">型，有的平台定义为<code class="literal"><span style="font-size: 10pt; ">unsigned long long</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">型，C标准规定<code class="literal"><span style="font-size: 10pt; ">size_t</span></code></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: 10pt; ">这个名字就是为了隐藏这些细节，使代码具有可移植性。<br /></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: medium; "><br /></span><span class="Apple-style-span" style="font-family: 'Times New Roman'; line-height: 20px; background-color: #ffffff; font-size: 10pt; ">&nbsp;&nbsp;&nbsp;typedef用来声明一个别名，typedef后面的语法，是一个声明。本来笔者以为这里不会产生什么误解的，但结果却出乎意料，产生误解的人不在少数。罪魁祸首又是那些害人的教材。在这些教材中介绍typedef的时候通常会写出如下形式： typedef int PARA; 这种形式跟#define int PARA几乎一样，这些教材的宗旨是由浅入深，但实际做出来的行为却是以偏盖全。的确，这种形式在所有形式中是最简单的，但却没有对 typedef进一步解释，使得不少人用#define的思维来看待typedef，把int与PARA分开来看，int是一部分，PARA是另一部分，但实际上根本就不是这么一回事。int与PARA是一个整体！就象int i:声明一样是一个整体声明，只不过int i定义了一个变量，而typedef定义了一个别名。这些人由于持有这种错误的观念，就会无法理解如下一些声明： typedef int a[10]; typedef void (*p)(void); 他们会以为a[10]是int的别名，(*p)(void)是void的别名，但这样的别名看起来又似乎不是合法的名字，于是陷入困惑之中。实际上，上面的语句把a声明为具有10个int元素的数组的类型别名，p是一种函数指针的类型别名。 虽然在功能上，typedef可以看作一个跟int PARA分离的动作，但语法上typedef属于存储类声明说明符，因此严格来说，typedef int PARA整个是一个完整的声明。 定义一个函数指针类型。 比如原函数是 void func(void); 那么定义的函数指针类型就是typedef void (*Fun)(void); 然后用此类型生成一个指向函数的指针： Fun func1; 当func1获取函数地址之后，那么你就可以向调用原函数那样来使用这个函数指针： func1(void);<br /></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: medium; "><span class="Apple-style-span" style="font-family: 'courier new', courier, fixed; font-size: 14px; white-space: pre; background-color: #f0f0f0; ">下面举几个例子：<br /></span></span><span class="Apple-style-span" style="font-family: arial, sans-serif, 宋体; line-height: 22px; background-color: #f5f8fd; "><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">typedef&nbsp;</span><span style="color: #0000FF; ">struct</span><span style="color: #000000; ">&nbsp;tagMyStruct<br />{&nbsp;<br />　</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;iNum;<br />　</span><span style="color: #0000FF; ">long</span><span style="color: #000000; ">&nbsp;lLength;<br />}&nbsp;MyStruct;</span></div>下一个<br /></p></span><span class="Apple-style-span" style="font-family: arial, sans-serif, 宋体; line-height: 22px; background-color: #f5f8fd; "><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">typedef&nbsp;</span><span style="color: #0000FF; ">struct</span><span style="color: #000000; ">&nbsp;tagNode<br />{<br />　</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pItem;<br />　pNode&nbsp;pNext;<br />}&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pNode;</span></div></span><span class="Apple-style-span" style="font-family: arial, sans-serif, 宋体; line-height: 22px; background-color: #f5f8fd; ">编译错误，<div style="display: inline-block; "></div></span><span class="Apple-style-span" style="font-family: arial, sans-serif, 宋体; line-height: 22px; background-color: #f5f8fd; ">C语言当然允许在结构中包含指向它自己的指针，我们可以在建立链表等数据结构的实现上看到无数这样的例子，上述代码的根本问题在于typedef的应用。正确的：<br /></span><span class="Apple-style-span" style="font-family: arial, sans-serif, 宋体; line-height: 22px; background-color: #f5f8fd; "><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">typedef&nbsp;</span><span style="color: #0000FF; ">struct</span><span style="color: #000000; ">&nbsp;tagNode&nbsp;<br />{<br />　</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pItem;<br />　</span><span style="color: #0000FF; ">struct</span><span style="color: #000000; ">&nbsp;tagNode&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pNext;<br />}&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pNode;<br /></span></div></p></span><span class="Apple-style-span" style="font-family: arial, sans-serif, 宋体; line-height: 22px; background-color: #f5f8fd; "><br /><div></div></span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; line-height: normal; background-color: #ffffff; font-size: medium; "><span class="Apple-style-span" style="font-family: 'courier new', courier, fixed; font-size: 14px; white-space: pre; background-color: #f0f0f0; "><br /></span><br /></span><img src ="http://www.cppblog.com/nxm1990/aggbug/160083.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/nxm1990/" target="_blank">is-programmer</a> 2011-11-14 14:12 <a href="http://www.cppblog.com/nxm1990/articles/160083.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ANSI C 的标准</title><link>http://www.cppblog.com/nxm1990/articles/160067.html</link><dc:creator>is-programmer</dc:creator><author>is-programmer</author><pubDate>Mon, 14 Nov 2011 04:40:00 GMT</pubDate><guid>http://www.cppblog.com/nxm1990/articles/160067.html</guid><wfw:comment>http://www.cppblog.com/nxm1990/comments/160067.html</wfw:comment><comments>http://www.cppblog.com/nxm1990/articles/160067.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/nxm1990/comments/commentRss/160067.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/nxm1990/services/trackbacks/160067.html</trackback:ping><description><![CDATA[<div>&nbsp;&nbsp; 大部分的关于C的著作都提到ANSI C Standard，但我相信少有C程序员真正细致阅读过ANSI C标准，C标准规定了一些原则，充分给了编译器程序员权力，根据具体平台的特性去决定一些规则。C标准的Rationale之一：<em>优先考虑效率，而可移植性尚在其次</em>。<br />Implementation-defined、Unspecified和Undefined<br />&nbsp;&nbsp; 在C标准中没有做明确规定的地方会用Implementation-defined、Unspecified或Undefined来表述，在本书中有时把这三种情况统称为&#8220;未明确定义&#8221;的。这三种情况到底有什么不同呢？<br />&nbsp;&nbsp; 我们刚才看到一种Implementation-defined的情况，C标准没有明确规定<code>char</code>是有符号的还是无符号的，但是要求编译器必须对此做出明确规定，并写在编译器的文档中。<br />&nbsp;&nbsp; 对于Unspecified的情况，往往有几种可选的处理方式，C标准没有明确规定按哪种方式处理，编译器可以自己决定，并且也不必写在编译器的文档中， 这样即便用同一个编译器的不同版本来编译也可能得到不同的结果，因为编译器没有在文档中明确写它会怎么处理，那么不同版本的编译器就可以选择不同的处理方 式，比如下一章我们会讲到一个函数调用的各个实参表达式按什么顺序求值是Unspecified的。<br />&nbsp;&nbsp; Undefined的情况则是完全不确定的，C标准没规定怎么处理，编译器很可能也没规定，甚至也没做出错处理，有很多Undefined的情况编译器是检查不出来的，最终会导致运行时错误，比如数组访问越界就是Undefined的。<br />&nbsp;&nbsp; 初学者看到这些规则通常会很不舒服，觉得这不是在学编程而是在啃法律条文，结果越学越泄气。是的，C语言并不像一个数学定理那么完美，现实世界里的东西总是 不够完美的。但还好啦，C程序员已经很幸福了，只要严格遵照C标准来写代码，不要去触碰那些阴暗角落，写出来的代码就有很好的可移植性。想想那些可怜的 JavaScript程序员吧，他们甚至连一个可以遵照的标准都没有，一个浏览器一个样，甚至同一个浏览器的不同版本也差别很大，程序员不得不为每一种浏 览器的每一个版本分别写不同的代码。<br /></div><img src ="http://www.cppblog.com/nxm1990/aggbug/160067.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/nxm1990/" target="_blank">is-programmer</a> 2011-11-14 12:40 <a href="http://www.cppblog.com/nxm1990/articles/160067.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>