﻿<?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++博客-flyonok-随笔分类-c++</title><link>http://www.cppblog.com/flyonok/category/13836.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 26 Aug 2011 23:11:18 GMT</lastBuildDate><pubDate>Fri, 26 Aug 2011 23:11:18 GMT</pubDate><ttl>60</ttl><item><title>boost 1.35.0 Visual Studio 2008编译指南</title><link>http://www.cppblog.com/flyonok/archive/2011/08/26/154399.html</link><dc:creator>flyonok</dc:creator><author>flyonok</author><pubDate>Fri, 26 Aug 2011 03:20:00 GMT</pubDate><guid>http://www.cppblog.com/flyonok/archive/2011/08/26/154399.html</guid><wfw:comment>http://www.cppblog.com/flyonok/comments/154399.html</wfw:comment><comments>http://www.cppblog.com/flyonok/archive/2011/08/26/154399.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyonok/comments/commentRss/154399.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyonok/services/trackbacks/154399.html</trackback:ping><description><![CDATA[<span style="color: black; font-family: arial, sans-serif, 宋体; font-size: 14px; line-height: 22px; background-color: #f5f8fd; "><h1>一。下载</h1><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">boost库2008年3月底发布了1.35.0版本，包括了12个新的库和若干对于新库的修正。请参见<a title="http://www.boost.org/users/news/version_1_35_0" href="http://www.boost.org/users/news/version_1_35_0" style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: #003195; text-decoration: none; ">http://www.boost.org/users/news/version_1_35_0</a>来获取详细的更新信息。</p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">最大的喜讯就是提供了对于Visual Studio 2008编译器的支持（1.34也可以，但是需要手工修改）。</p><blockquote style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">下载地址：</strong>&nbsp;<br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " /><a title="http://www.boost.org/users/download/" href="http://www.boost.org/users/download/" style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: #003195; text-decoration: none; ">http://www.boost.org/users/download/</a></p></blockquote><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">boost JAM可以不用下载，源代码已经包括在boost库中，可以直接来编译出来。</p><h1>二。准备工作</h1><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">编译boost库之前，需要做一些准备工作。下载一些Open Source的包，来支持boost特定库的需要。</p><h2>1. ICU</h2><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">ICU提供了unicode和国际化支持，目前的版本是3.8.1。ICU的主页是<a title="http://www.icu-project.org/" href="http://www.icu-project.org/" style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: #003195; text-decoration: none; ">http://www.icu-project.org/</a>。</p><h3>(1). 下载</h3><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">可以从<a title="http://www.icu-project.org/download/3.8.html" href="http://www.icu-project.org/download/3.8.html" style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: #003195; text-decoration: none; ">http://www.icu-project.org/download/3.8.html</a>下载源代码版本和使用VS2005编译的版本。推荐下载源代码版本自己进行编译，以避免依赖于VS2005的运行时库。</p><h3>(2). 编译</h3><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">ICU的编译比较简单，打开ICU源代码目录下的sourceallinoneallinone.sln，需要转换到VS2008格式，直接转换即可。然后，选择release，Rebuild Solution即可。</p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">(3). 测试</p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">将编译出来的bin目录加入到系统的PATH目录中去。然后，重新打开allinone.sln工程。</p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">需要通过测试的项目</p><ol style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><li style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">cintltst项目</li><li style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">intltest项目</li><li style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">iotest</li></ol><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">分别设置成启动项目，运行即可。</p><h2>2. bzip</h2><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">bzip的主页是&nbsp;<a title="http://www.bzip.org/" href="http://www.bzip.org/" style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: #003195; text-decoration: none; ">http://www.bzip.org/</a>，从<a title="http://www.bzip.org/downloads.html" href="http://www.bzip.org/downloads.html" style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: #003195; text-decoration: none; ">http://www.bzip.org/downloads.html</a>下面下载源代码包即可，boost直接使用源代码来进行编译。</p><h2>3. zlib</h2><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">zlib的主页是<a title="http://www.zlib.net/" href="http://www.zlib.net/" style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: #003195; text-decoration: none; ">http://www.zlib.net/</a>，从该网页下面下载源代码包即可，boost直接使用源代码来进行编译。</p><h2>4. python</h2><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">python的主页是<a title="http://www.python.org/" href="http://www.python.org/" style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: #003195; text-decoration: none; ">http://www.python.org/</a>，下载python的2.5.2版本，安装即可。boost默认是会编译python，并且会自动寻找python的安装目录。</p><h1>三。编译</h1><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">到开始菜单的VS2008菜单项下，启动Visual Studio 2008 Command Prompt，以下编译步骤均假定直接在该工具下进行编译。</p><h2>1. 编译Boost.Build</h2></span><span style="color: black; font-family: sans-serif; font-size: medium; "><p style="text-align: left; font-size: 10pt; line-height: 1.15; "><a external"="" href="http://www.boost.org/doc/libs/1_47_0/tools/build/index.html" style="text-decoration: none; color: #005a9c; ">Boost.Build</a>&nbsp;is a text-based system for developing, testing, and installing software. First, you'll need to build and install it. To do this:</p><ol simple"="" style="text-align: left; margin-bottom: 1em; list-style-type: decimal; list-style-position: initial; list-style-image: initial; "><li style="font-size: 10pt; line-height: 1.3; ">Go to the directory&nbsp;<tt literal"="">tools</tt><tt literal"="">\</tt><tt literal"="">build</tt><tt literal"="">\</tt><tt literal"="">v2</tt><tt literal"="">\</tt>.</li><li style="font-size: 10pt; line-height: 1.3; ">Run&nbsp;<tt literal"="">bootstrap.bat</tt></li><li style="font-size: 10pt; line-height: 1.3; ">Run&nbsp;<tt literal"="">b2 install&nbsp;<span style="white-space: pre; ">--prefix=</span></tt><em>PREFIX</em>&nbsp;where&nbsp;<em>PREFIX</em>&nbsp;is the directory where you want Boost.Build to be installed</li><li style="font-size: 10pt; line-height: 1.3; ">Add&nbsp;<em>PREFIX</em><tt literal"="">\</tt><tt literal"="">bin</tt>&nbsp;to your PATH environment variable.</li></ol></span><span style="font-family: arial, sans-serif, 宋体; font-size: 14px; line-height: 22px; background-color: #f5f8fd; "><h2>2. 编译boost</h2><p style="color: black; font-family: arial, sans-serif, 宋体; line-height: 22px; ">鉴于参考2下使用build-type有问题，可以使用以下命令来分别编译dll版本和lib版本。下面是一个示例脚本的例子，其中的目录需要替换:</p></span><span style="background-color: #f5f8fd; "><blockquote><div style="line-height: 21px; border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 539px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; ">&nbsp;1<img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />REM&nbsp;used&nbsp;with&nbsp;iostream&nbsp;library&nbsp;<br />&nbsp;2<img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />REM&nbsp;boost_1_35_0libsiostreamsdocinstallation.html&nbsp;<br />&nbsp;3<img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br />&nbsp;4<img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />set&nbsp;BZIP2_SOURCE="E:libraryzip2-1.0.4"&nbsp;<br />&nbsp;5<img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />set&nbsp;ZLIB_SOURCE="E:libraryzlib123"&nbsp;<br />&nbsp;6<img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br />&nbsp;7<img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />REM&nbsp;used&nbsp;with&nbsp;regex&nbsp;library&nbsp;with&nbsp;unicode&nbsp;support&nbsp;<br />&nbsp;8<img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />set&nbsp;ICU_PATH="E:libraryicu"<br />&nbsp;9<img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br />10<img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " />REM&nbsp;DLL版本&nbsp;<br />11 &nbsp;&nbsp;</div><div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 539px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; "><div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 539px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; "><span style="line-height: 21px;"><span style="white-space:pre">	</span>b2 --build-dir=e:\boost-dist toolset=msvc-9.0 --buildtype=complete</span></div><div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 539px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; "><span style="line-height: 21px;"><span style="white-space:pre">	</span> --prefix=d:\boost link=shared threading=multi&nbsp;</span></div><div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 539px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; "><span style="line-height: 21px;"><span style="white-space: pre; ">	</span> runtime-link=shared install</span></div></div></blockquote></span><span style="background-color: #eeeeee; line-height: 21px; "><span style="white-space:pre">	</span></span><div><span style="background-color: #f5f8fd; "><blockquote><div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 539px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; "><span style="line-height: 21px; font-size: 13px; font-family: arial, sans-serif, 宋体; color: #008080; ">12</span><span style="line-height: 21px; font-size: 13px; font-family: arial, sans-serif, 宋体; color: #000000; "><img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; line-height: 21px; font-family: arial, sans-serif, 宋体; " /><br style="line-height: 21px; font-family: arial, sans-serif, 宋体; " /></span><span style="line-height: 21px; font-size: 13px; font-family: arial, sans-serif, 宋体; color: #008080; ">13</span><span style="line-height: 21px; font-size: 13px; font-family: arial, sans-serif, 宋体; color: #000000; "><img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; line-height: 21px; font-family: arial, sans-serif, 宋体; " />REM&nbsp;lib版本&nbsp;<br style="line-height: 21px; font-family: arial, sans-serif, 宋体; " /></span><span style="line-height: 21px; font-size: 13px; font-family: arial, sans-serif, 宋体; color: #008080; ">14 &nbsp;&nbsp;</span></div><div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 539px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; "><div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 539px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; "><span style="line-height: 21px;">&nbsp; b2 --build-dir=e:\boost-dist toolset=msvc-9.0 --buildtype=complete</span></div><div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 539px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; "><span style="line-height: 21px;"><span style="white-space:pre">	</span> --prefix=d:\boost link=static threading=multi&nbsp;</span></div><div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 539px; word-break: break-all; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee; "><span style="line-height: 21px;"><span style="white-space: pre; ">	</span> runtime-link=shared install</span></div></div><img src="http://www.kuqin.com/upimg/allimg/080408/0049520.gif" align="top" alt="" style="font-family: arial, sans-serif, 宋体; line-height: 21px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></blockquote></span><span style="font-family: arial, sans-serif, 宋体; font-size: 14px; font-weight: bold; line-height: 45px; background-color: #f5f8fd; ">四。参考文章</span><span style="background-color: #f5f8fd; "><blockquote><p style="color: black; font-size: 14px; font-family: arial, sans-serif, 宋体; line-height: 22px; ">&nbsp;</p></blockquote></span><span style="background-color: #f5f8fd; "><p style="color: black; font-size: 14px; font-family: arial, sans-serif, 宋体; line-height: 22px; ">1. vs2005下安装boost&nbsp;<br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " /><a title="http://hi.baidu.com/vivid217/blog/item/8e484eafd3b52ff8faed5063.html" href="http://hi.baidu.com/vivid217/blog/item/8e484eafd3b52ff8faed5063.html" style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: #003195; text-decoration: none; ">http://hi.baidu.com/vivid217/blog/item/8e484eafd3b52ff8faed5063.html</a></p><p style="color: black; font-size: 14px; font-family: arial, sans-serif, 宋体; line-height: 22px; ">2. boost1.35.0编译日志&nbsp;<br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " /><a title="http://www.cppblog.com/chemz/archive/2008/03/31/45822.html" href="http://www.cppblog.com/chemz/archive/2008/03/31/45822.html" style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: #003195; text-decoration: none; ">http://www.cppblog.com/chemz/archive/2008/03/31/45822.html</a></p><p style="color: black; font-size: 14px; font-family: arial, sans-serif, 宋体; line-height: 22px; ">来自：<a href="http://www.cppblog.com/arthurlee/archive/2008/04/07/46427.html" style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: #003195; text-decoration: none; ">http://www.cppblog.com/arthurlee/archive/2008/04/07/46427.html</a></p></span></div><img src ="http://www.cppblog.com/flyonok/aggbug/154399.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyonok/" target="_blank">flyonok</a> 2011-08-26 11:20 <a href="http://www.cppblog.com/flyonok/archive/2011/08/26/154399.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于这样 gcc -O3 和-O2 -Os 的说法对吗</title><link>http://www.cppblog.com/flyonok/archive/2010/11/25/134649.html</link><dc:creator>flyonok</dc:creator><author>flyonok</author><pubDate>Thu, 25 Nov 2010 08:54:00 GMT</pubDate><guid>http://www.cppblog.com/flyonok/archive/2010/11/25/134649.html</guid><wfw:comment>http://www.cppblog.com/flyonok/comments/134649.html</wfw:comment><comments>http://www.cppblog.com/flyonok/archive/2010/11/25/134649.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyonok/comments/commentRss/134649.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyonok/services/trackbacks/134649.html</trackback:ping><description><![CDATA[<span class=Apple-style-span style="WORD-SPACING: 0px; FONT: medium Simsun; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; orphans: 2; widows: 2; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span class=Apple-style-span style="FONT-SIZE: 12px; FONT-FAMILY: verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif, simsun, 宋体, bitstream, Nimbus; TEXT-ALIGN: left">在 x86 系列cpu采用 -O3 并不是一种好的选择。因为 x86 系列cpu缺乏足够寄存器，-O3 会导致代码变慢。<br><br>另外，-O3 包含了 -finline-functions，这会使目标代码不必要地膨胀，从而导致执行变慢。<br><br>所以在 x86 系列 cpu 采用 -O2 和 -Os 的效果比 -O3 好。<br><br>建议 CFLAGS 设置如下：<br>CFLAGS="-O2 -march=pentium4 -pipe"<br><br><br>/etc/make.conf 中 LDFLAGS 的设置对性能有较大的影响。<br><br>1、基础优化：<br><br>LDFLAGS="-Wl,-O1"<br><br>这种优化设置非常安全，不会有什么问题。<br><br>2、通过"—as-needed"加快软件载入速度：<br><br>LDFLAGS="-Wl,-O1 -Wl,--as-needed"<br><br>在这种优化设置中，加入了--as-needed参数，这将避免软件连接多余的库，从而大大提升软件的载入速度，特别是 KDE 和 GNOME 这类大型软件的载入速度。<br>这是我喜爱的一种优化方案。<br>需要注意的是，由于有些软件包不支持 --as-needed 参数而导致 emerge 失败，在构造这类软件包时需要取消 --as-needed 参数。</span></span>
<img src ="http://www.cppblog.com/flyonok/aggbug/134649.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyonok/" target="_blank">flyonok</a> 2010-11-25 16:54 <a href="http://www.cppblog.com/flyonok/archive/2010/11/25/134649.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>win32 和 linux的相关API</title><link>http://www.cppblog.com/flyonok/archive/2010/10/11/129326.html</link><dc:creator>flyonok</dc:creator><author>flyonok</author><pubDate>Mon, 11 Oct 2010 02:46:00 GMT</pubDate><guid>http://www.cppblog.com/flyonok/archive/2010/10/11/129326.html</guid><wfw:comment>http://www.cppblog.com/flyonok/comments/129326.html</wfw:comment><comments>http://www.cppblog.com/flyonok/archive/2010/10/11/129326.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyonok/comments/commentRss/129326.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyonok/services/trackbacks/129326.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: linux win32 API&nbsp;&nbsp;<a href='http://www.cppblog.com/flyonok/archive/2010/10/11/129326.html'>阅读全文</a><img src ="http://www.cppblog.com/flyonok/aggbug/129326.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyonok/" target="_blank">flyonok</a> 2010-10-11 10:46 <a href="http://www.cppblog.com/flyonok/archive/2010/10/11/129326.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Asio资料收集及使用点滴</title><link>http://www.cppblog.com/flyonok/archive/2010/09/29/128020.html</link><dc:creator>flyonok</dc:creator><author>flyonok</author><pubDate>Wed, 29 Sep 2010 02:08:00 GMT</pubDate><guid>http://www.cppblog.com/flyonok/archive/2010/09/29/128020.html</guid><wfw:comment>http://www.cppblog.com/flyonok/comments/128020.html</wfw:comment><comments>http://www.cppblog.com/flyonok/archive/2010/09/29/128020.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyonok/comments/commentRss/128020.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyonok/services/trackbacks/128020.html</trackback:ping><description><![CDATA[<span class=Apple-style-span style="WORD-SPACING: 0px; FONT: medium Simsun; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; orphans: 2; widows: 2; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span class=Apple-style-span style="FONT-SIZE: 13px; LINE-HEIGHT: 16px; FONT-FAMILY: Verdana, Geneva, Arial, Helvetica, sans-serif">
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">Asio的架构：<a style="COLOR: navy; TEXT-DECORATION: none" href="http://blog.163.com/waft_xu/blog/static/438152942008828115542490/"><strong>Boost.Asio 设计索引</strong></a></p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">概念性了解API：<a style="COLOR: navy; TEXT-DECORATION: none" href="http://blog.csdn.net/ryong21/archive/2008/05/21/2467206.aspx"><strong>boost::asio中的同步与异步</strong></a></p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">Asio的Buffer:<span class=Apple-converted-space>&nbsp;</span><strong><a style="COLOR: navy; TEXT-DECORATION: none" href="http://blog.chinaunix.net/u3/102778/showart_2019368.html">buffer几种用法</a></strong>,这些Buffer都只是引用外部的内存数据，如果需要拷贝和分配，记得使用boost::pool，这里还有一篇处理拷贝Buffer的<a style="COLOR: navy; TEXT-DECORATION: none" href="http://bbs.rosoo.net/redirect.php?tid=83&amp;goto=lastpost">文章</a></p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">例子解析:<span class=Apple-converted-space>&nbsp;</span><a style="COLOR: navy; TEXT-DECORATION: none" href="http://blog.163.com/miky_sun/blog/static/336940520104176206401/"><strong>Boost.asio的简单使用(timer,thread,io_service类)</strong></a></p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">如果照着例子弄出的第一个服务器无法收到客户端消息，试试这个<a style="COLOR: navy; TEXT-DECORATION: none" href="http://www.cppblog.com/ngaut/archive/2007/06/29/27221.html"><strong>asio::async_read与socket的async_read_some的区别</strong></a></p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">这里是另外一个区别：<a style="COLOR: navy; TEXT-DECORATION: none" href="http://www.cnblogs.com/Frodo/archive/2009/06/16/1504052.html"><strong>boost.asio库学习笔记—— receive和read的区别</strong>：</a></p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">&nbsp;</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">从服务器连接过来的客户端的地址：</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">std::string endpoint = socket.remote_endpoint( ).address( ).to_string();</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">以下是对这篇<a style="COLOR: navy; TEXT-DECORATION: none" href="http://www.cppblog.com/ngaut/archive/2007/04/13/21751.html">文章</a>的翻译:</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">asio chat_client.cpp中的一些问题</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">1. 有多少个线程在运行2个，还是3个？</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">&gt;一般来说，依赖于运行的平台，从程序的角度来说是2个，包括：</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">*主线程，用于处理用户的输入输出</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">*io_service.run()线程，用于处理chat_client对象中的所有行为(action)</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">还有，async_write会创建一个线程或者其他的一些东西么？</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">&gt;不会.</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">2. 有关1的问题，为什么write函数使用post直接调用？什么不调用async_write？既然调用了post，你只是将其放到一个队列里在同一线程处理，为什么之后还要从其他线程调用async_write？</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">chat_client的成员对象不是线程安全的（故意？），因此要同步处理这些成员。如果直接从主线程调用async_write不是线程安全的，因为此时可能有后台线程正在访问socket。</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">在这个例子中，所有的类成员都调用io_services.post()以保证在一个线程里访问，达到线程安全。io_services保证任何使用io_services.post()（或io_servies.dispatch())传入的句柄只会在io_serive.run()线程被调用。而且这个例子中只有一个线程调用io_service.run()，所以chat_client的成员变量也只会在一个线程中被访问。</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">4. 如果我想发送一个连接事件到主线程，怎样做？用io_service::post？能从主线程获取io_services？</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">在这个例子里是很困难的，因为主线程正在阻塞等待用户信息。不过如果你想将事件在线程间传递，确实可以为每个线程配备一个io_services。</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">5. 为什么在main函数的最后调用了t.join()，能用io_service.run()代替么？</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">不行，请参考问题2的解答，那样的话，线程安全将无法保证</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">6. 按照问题1的解答，如果有3个线程在运行（也就是，async_write被放到另外一个线程），那么哪个位置创建这个线程比较好？</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">因为主线程需要阻塞等待用户信息，因此io_service::run是唯一需要的。如果你的程序不需要这样做，那么就不需要其他线程，也就只需要简单的调用io_service::run()就可以了，这也是大多数例子这样做的原因</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">&nbsp;</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px"><strong>有关线程安全的问题</strong></p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">1. 对于asio对象，能从2个不同线程调用一个共享对象的不同成员么？</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">不能</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">那么其意义就是从2个不同线程访问共享对象不是线程安全的？</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">是的</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">只有被标记为 &#8220;共享对象：安全&#8221;的对象才能从不同线程同时访问，io_service就是这样的对象</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">2. 同样是线程安全的问题，对于basic_deadline_timer::cancel()我需要用io_service.post(boost::bind(&amp;deadline_timer::cancel, &amp;myTimer))方法封装调用么？</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">是的，直接调用cancel()也不是线程安全的</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">最好的解决方法就是使用io_service::post()将所有的操作都放在一个线程</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">3. asio有很多成员函数，我怎么知道哪些能安全的调用？</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">一般情况下，你应该认为没有任何一个函数是安全的，以下是通用的io线程安全判断用例：</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">write+write:不安全</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">read+write:不安全</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">read+read:安全</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">asio对象已经符合这种需求</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">&nbsp;</p>
<p style="FONT-SIZE: 13px; MARGIN: 10px 0px; TEXT-INDENT: 20px; LINE-HEIGHT: 19px">这里<a style="COLOR: navy; TEXT-DECORATION: none" href="http://hi.baidu.com/jrckkyy/blog/item/e86835d61e60722506088b6a.html">有一篇</a>介绍io_service众多区别及包处理，拆包等的技术</p>
</span></span>
<img src ="http://www.cppblog.com/flyonok/aggbug/128020.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyonok/" target="_blank">flyonok</a> 2010-09-29 10:08 <a href="http://www.cppblog.com/flyonok/archive/2010/09/29/128020.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++的类型转换</title><link>http://www.cppblog.com/flyonok/archive/2010/08/16/123564.html</link><dc:creator>flyonok</dc:creator><author>flyonok</author><pubDate>Mon, 16 Aug 2010 02:17:00 GMT</pubDate><guid>http://www.cppblog.com/flyonok/archive/2010/08/16/123564.html</guid><wfw:comment>http://www.cppblog.com/flyonok/comments/123564.html</wfw:comment><comments>http://www.cppblog.com/flyonok/archive/2010/08/16/123564.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyonok/comments/commentRss/123564.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyonok/services/trackbacks/123564.html</trackback:ping><description><![CDATA[
<h1 class="cjk"><font face="AR PL UMing HK, sans-serif"><font face="Times New Roman">1:</font></font><font face="Times New Roman">老式类型转换</font></h1>
<p style="margin-bottom: 0cm; border: none; padding: 0cm; line-height: 0.48cm; widows: 2; orphans: 2">
<font color="#000000"><font face="Arial"><font size="2" style="font-size: 9pt">风格（</font></font><font face="AR PL UMing HK, serif"><font face="Arial"><font size="2" style="font-size: 9pt">C-style</font></font></font><font face="AR PL UMing HK, serif"></font><font face="Arial"><font size="2" style="font-size: 9pt">）强制转型如下：</font></font><font face="AR PL UMing HK, serif"><font face="Arial"><font size="2" style="font-size: 9pt">(T)
exdivssion // cast exdivssion to be of type T;</font></font></font><font face="AR PL UMing HK, serif"></font></font></p>
<p style="margin-bottom: 0cm; border: none; padding: 0cm; line-height: 0.48cm; widows: 2; orphans: 2">
<font color="#000000"><font face="Arial"><font size="2" style="font-size: 9pt">函数风格（</font></font><font face="AR PL UMing HK, serif"><font face="Arial"><font size="2" style="font-size: 9pt">Function-style</font></font></font><font face="AR PL UMing HK, serif"></font><font face="Arial"><font size="2" style="font-size: 9pt">）强制转型使用这样的语法：</font></font><font face="AR PL UMing HK, serif"><font face="Arial"><font size="2" style="font-size: 9pt">T(exdivssion)
// cast exdivssion to be of type T;</font></font></font><font face="AR PL UMing HK, serif"></font></font></p>
<h1 class="cjk"><font face="AR PL UMing HK, sans-serif"><font color="#000000"><font face="Arial"><font size="4" style="font-size: 16pt">2:</font></font></font></font><font color="#000000"><font face="Arial"><font size="4" style="font-size: 16pt">使用标准</font></font></font><font face="AR PL UMing HK, sans-serif"><font color="#000000"><font face="Arial"><font size="4" style="font-size: 16pt">C++</font></font></font></font><font color="#000000"><font face="Arial"><font size="4" style="font-size: 16pt">的类型转换符：</font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">标准</font></font></font><font face="AR PL UMing HK, sans-serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">C++</font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">中有四个类型转换符：</font></font></font><font face="AR PL UMing HK, sans-serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><strong>static_cast</strong></font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">、</font></font></font><font face="AR PL UMing HK, sans-serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><strong>dynamic_cast</strong></font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">、</font></font></font><font face="AR PL UMing HK, sans-serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><strong>reinterpret_cast</strong></font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">、和</font></font></font><font face="AR PL UMing HK, sans-serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><strong>const_cast</strong></font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">。下面对它们一一进行介绍。</font></font></font></h1>
<h2 class="cjk"><font color="#000000">&nbsp;</font><font face="AR PL UMing HK, sans-serif"><font color="#000000"><font face="Tahoma, Arial"><font size="4" style="font-size: 16pt"><strong>static_cast</strong></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br></font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">用法：</font></font></font><font face="AR PL UMing HK, sans-serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><strong>static_cast&nbsp;</strong></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">&lt;
type-id &gt; ( expression
)<br><br></font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">该运算符把</font></font></font><font face="AR PL UMing HK, sans-serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">expression</font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">转换为</font></font></font><font face="AR PL UMing HK, sans-serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">type-id</font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">类型，但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法：</font></font></font></h2>
<ul>
	<li><p align="LEFT" style="margin-right: 0.03cm; margin-top: 0.03cm; margin-bottom: 0.03cm; border: none; padding: 0cm; font-style: normal; font-weight: normal; line-height: 0.5cm; widows: 2; orphans: 2">
	<font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">用于类层次结构中基类和子类之间指针或引用的转换。进行上行转换（把子类的指针或引用转换成基类表示）是安全的；进行下行转换（把基类指针或引用转换成子类表示）时，由于没有动态类型检查，所以是不安全的。</font></font></font></p>
	</li><li><p align="LEFT" style="margin-right: 0.03cm; margin-top: 0.03cm; margin-bottom: 0.03cm; border: none; padding: 0cm; line-height: 0.5cm; widows: 2; orphans: 2">
	<font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">用于基本数据类型之间的转换，如把</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">int</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">转换成</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">char</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">，把</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">int</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">转换成</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">enum</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">。这种转换的安全性也要开发人员来保证。</font></font></font></p>
	</li><li><p align="LEFT" style="margin-right: 0.03cm; margin-top: 0.03cm; margin-bottom: 0.03cm; border: none; padding: 0cm; font-style: normal; font-weight: normal; line-height: 0.5cm; widows: 2; orphans: 2">
	<font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">把空指针转换成目标类型的空指针。</font></font></font></p>
	</li><li><p align="LEFT" style="margin-right: 0.03cm; margin-top: 0.03cm; margin-bottom: 0.03cm; border: none; padding: 0cm; line-height: 0.5cm; widows: 2; orphans: 2">
	<font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">把任何类型的表达式转换成</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">void</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">类型。</font></font></font></p>
</li></ul>
<p align="LEFT" style="margin-bottom: 0cm; line-height: 0.5cm; widows: 2; orphans: 2">
<font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">注意：</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">static_cast</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">不能转换掉</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">expression</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">的</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">const</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">、</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">volitale</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">、或者</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">__unaligned</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">属性。</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br></font></font><font face="Tahoma, Arial"><font size="4" style="font-size: 16pt"><strong>dynamic_cast</strong></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">用法：</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><strong>dynamic_cast&nbsp;</strong></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">&lt;
type-id &gt; ( expression
)<br><br></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">该运算符把</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">expression</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">转换成</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">type-id</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">类型的对象。</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">Type-id</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">必须是类的指针、类的引用或者</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">void
*</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">；如果</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">type-id</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">是类指针类型，那么</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">expression</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">也必须是一个指针，如果</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">type-id</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">是一个引用，那么</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">expression</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">也必须是一个引用。</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br>dynamic_cast</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">主要用于类层次间的上行转换和下行转换，还可以用于类之间的交叉转换。</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">在类层次间进行上行转换时，</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">dynamic_cast</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">和</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">static_cast</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">的效果是一样的；在进行下行转换时，</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">dynamic_cast</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">具有类型检查的功能，比</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">static_cast</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">更安全。</font></font></font></p>
<p align="LEFT" style="margin-left: 0.13cm; margin-right: 0.13cm; margin-top: 0.13cm; margin-bottom: 0.13cm; border: none; padding: 0cm; line-height: 0.5cm; widows: 2; orphans: 2">
<font color="#000000">&nbsp;<font face="AR PL UMing HK, serif"><font face="Courier New"><font size="2" style="font-size: 9pt">class
B{<br><br>public:<br><br>&nbsp;int m_iNum;<br><br>&nbsp;virtual void
foo();<br><br>};<br><br>class D:public B{<br><br>&nbsp;public:<br><br>&nbsp;char
*m_szName[100];<br><br>};<br><br>&nbsp;<br><br>void func(B *pb){<br><br>&nbsp;D
*pd1 = static_cast&lt;D *&gt;(pb);<br><br>&nbsp;D *pd2 =
dynamic_cast&lt;D *&gt;(pb);<br><br>}</font></font></font><font face="AR PL UMing HK, serif"></font></font></p>
<p align="LEFT" style="margin-bottom: 0cm; line-height: 0.5cm; widows: 2; orphans: 2">
<font color="#000000"><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">在上面的代码段中，如果</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">pb</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">指向一个</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">D</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">类型的对象，</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">pd1</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">和</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">pd2</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">是一样的，并且对这两个指针执行</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">D</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">类型的任何操作都是安全的；但是，如果</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">pb</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">指向的是一个</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">B</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">类型的对象，那么</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">pd1</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">将是一个指向该对象的指针，对它进行</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">D</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">类型的操作将是不安全的（如访问</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">m_szName</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">），而</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">pd2</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">将是一个空指针。另外要注意：</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">B</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">要有虚函数，否则会编译出错；</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">static_cast</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">则没有这个限制。这是由于运行时类型检查需要运行时类型信息，而这个信息存储在类的虚函数表（关于虚函数表的概念，详细可见</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">&lt;Inside
c++ object
model&gt;</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">）中，只有定义了虚函数的类才有虚函数表，没有定义虚函数的类是没有虚函数表的。</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">另外，</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><strong>dynamic_cast</strong></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">还支持交叉转换（</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">cross
cast</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">）。如下代码所示。</font></font></font></p>
<p align="LEFT" style="margin-left: 0.13cm; margin-right: 0.13cm; margin-top: 0.13cm; margin-bottom: 0.13cm; border: none; padding: 0cm; line-height: 0.5cm; widows: 2; orphans: 2">
<font color="#000000"><font face="Courier New"><font size="2" style="font-size: 9pt">class
A{<br><br>public:<br><br>&nbsp;int m_iNum;<br><br>&nbsp;virtual void
f(){}<br><br>};<br><br>&nbsp;<br><br>class B:public A{<br><br>};<br><br>&nbsp;<br><br>class
D:public A{<br><br>};<br><br>&nbsp;<br><br>void foo(){<br><br>&nbsp;B
*pb = new B;<br><br>&nbsp;pb-&gt;m_iNum = 100;<br><br>&nbsp;D *pd1 =
static_cast&lt;D *&gt;(pb); //copile error<br><br>&nbsp;D *pd2 =
dynamic_cast&lt;D *&gt;(pb); //pd2 is NULL<br><br>&nbsp;delete pb;<br><br>}</font></font></font></p>
<p align="LEFT" style="margin-bottom: 0cm; line-height: 0.5cm; widows: 2; orphans: 2">
<font color="#000000"><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">在函数</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">foo</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">中，使用</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><strong>static_cast</strong></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">进行转换是不被允许的，将在编译时出错；而使用&nbsp;</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><strong>dynamic_cast</strong></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">的转换则是允许的，结果是空指针。</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br></font></font><font face="Tahoma, Arial"><font size="4" style="font-size: 16pt"><strong>reinpreter_cast</strong></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">用法：</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><strong>reinpreter_cast</strong></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">&lt;type-id&gt;
(expression)<br><br>type-id</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">必须是一个指针、引用、算术类型、函数指针或者成员指针。它可以把一个指针转换成一个整数，也可以把一个整数转换成一个指针（先把一个指针转换成一个整数，在把该整数转换成原类型的指针，还可以得到原先的指针值）。</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">该运算符的用法比较多。</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br></font></font><font face="Tahoma, Arial"><font size="4" style="font-size: 16pt"><strong>const_cast</strong></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">用法：</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><strong>const_cast</strong></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">&lt;type_id&gt;
(expression)<br><br></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">该运算符用来修改类型的</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">const</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">或</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">volatile</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">属性。除了</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">const
</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">或</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">volatile</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">修饰之外，
</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">type_id</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">和</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">expression</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">的类型是一样的。</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br></font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">常量指针被转化成非常量指针，并且仍然指向原来的对象；常量引用被转换成非常量引用，并且仍然指向原来的对象；常量对象被转换成非常量对象。</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br><br>Voiatile</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">和</font></font><font face="AR PL UMing HK, serif"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">const</font></font></font><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">类试。举如下一例：</font></font></font></p>
<p align="LEFT" style="margin-left: 0.13cm; margin-right: 0.13cm; margin-top: 0.13cm; margin-bottom: 0.13cm; border: none; padding: 0cm; line-height: 0.5cm; widows: 2; orphans: 2">
<font color="#000000"><font face="Courier New"><font size="2" style="font-size: 9pt">class
B{<br><br>&nbsp;public:<br><br>&nbsp;int m_iNum;<br><br>}<br><br>void
foo(){<br><br>const B b1;<br><br>b1.m_iNum = 100; //comile error<br><br>B
b2 = const_cast&lt;B&gt;(b1);<br><br>b2. m_iNum = 200; //fine<br>&nbsp;}</font></font></font></p>
<p align="LEFT" style="margin-bottom: 0cm; line-height: 0.5cm; widows: 2; orphans: 2">
<font face="AR PL UMing HK, serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt"><br></font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">上面的代码编译时会报错，因为</font></font></font><font face="AR PL UMing HK, serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">b1</font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">是一个常量对象，不能对它进行改变；使用</font></font></font><font face="AR PL UMing HK, serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">const_cast</font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">把它转换成一个常量对象，就可以对它的数据成员任意改变。注意：</font></font></font><font face="AR PL UMing HK, serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">b1</font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">和</font></font></font><font face="AR PL UMing HK, serif"><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">b2</font></font></font></font><font color="#000000"><font face="Tahoma, Arial"><font size="2" style="font-size: 9pt">是两个不同的对象。</font></font></font></p>

	
<img src ="http://www.cppblog.com/flyonok/aggbug/123564.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyonok/" target="_blank">flyonok</a> 2010-08-16 10:17 <a href="http://www.cppblog.com/flyonok/archive/2010/08/16/123564.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>各种语言的接口继承</title><link>http://www.cppblog.com/flyonok/archive/2010/07/08/119659.html</link><dc:creator>flyonok</dc:creator><author>flyonok</author><pubDate>Wed, 07 Jul 2010 16:36:00 GMT</pubDate><guid>http://www.cppblog.com/flyonok/archive/2010/07/08/119659.html</guid><wfw:comment>http://www.cppblog.com/flyonok/comments/119659.html</wfw:comment><comments>http://www.cppblog.com/flyonok/archive/2010/07/08/119659.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/flyonok/comments/commentRss/119659.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyonok/services/trackbacks/119659.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/flyonok/archive/2010/07/08/119659.html'>阅读全文</a><img src ="http://www.cppblog.com/flyonok/aggbug/119659.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyonok/" target="_blank">flyonok</a> 2010-07-08 00:36 <a href="http://www.cppblog.com/flyonok/archive/2010/07/08/119659.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++ memory test</title><link>http://www.cppblog.com/flyonok/archive/2010/05/18/115658.html</link><dc:creator>flyonok</dc:creator><author>flyonok</author><pubDate>Tue, 18 May 2010 03:54:00 GMT</pubDate><guid>http://www.cppblog.com/flyonok/archive/2010/05/18/115658.html</guid><wfw:comment>http://www.cppblog.com/flyonok/comments/115658.html</wfw:comment><comments>http://www.cppblog.com/flyonok/archive/2010/05/18/115658.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyonok/comments/commentRss/115658.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyonok/services/trackbacks/115658.html</trackback:ping><description><![CDATA[<span style="FONT-SIZE: 14pt">Question #1 When will this line fail to compile: new myObj[100]; <br>a) Never <br>b) When myObj is too large to fit into memory <br>c) When myObj has no default constructor <br><br>Question #2 Assuming that myObj is less than 1000 bytes, is there anything wrong with this code? <br>char x[1000];<br>&nbsp;myObj *obj = reinterpret_cast<myobj *>(x); <br>new (obj) myObj;<br>&nbsp;a) Nope, it works fine <br>b) Yes, there could be byte alignment issues <br>c) Yes, the syntax for calling new is incorrect <br><br>Question #3 What is the functional difference between <br>myObj *x = new myObj[100]; delete x; and myObj *x = new myObj[100]; delete [] x;<br>&nbsp;a) There is none; they both work as expected <br>b) They both do nothing. <br>c) The first will not invoke all myObj destructors <br><br>Question #4 What is wrong with the following code? int * x = (int *) malloc(100 * sizeof(int)); x = realloc(x, sizeof(int) * 200); <br>a) If realloc fails, then the original memory is lost <br>b) Nothing, realloc is guaranteed to succeed (by returning the original pointer) <br>c) Nothing, realloc frees the original memory passed to it&nbsp; <br><br>Question#5 What is one possible symptom of having called free on the same block of memory twice? <br>a) Nothing, calling free twice on one block of memory always works fine <br>b) Malloc might always return the same block of memory <br>c) You cannot free any memory in the future&nbsp; <br><br>Question#6 What's wrong with this line of code? int *x = NULL; delete x; <br>a) It causes a segmentation fault when delete tries to access NULL <br>b) Nothing <br>c) It is undefined behavior <br><br>Question #7 Assuming this code were being compiled using a standards-conforming C++, what is wrong with it? int * x = malloc(100 * sizeof(int)); x = realloc(x, sizeof(int) * 200); <br>a) malloc is undefined in C++, you can only allocate memory using new <br>b) Invalid cast from a void* to an int* <br>c) Nothing is wrong with this code <br><br>Question #8 What is the default behavior of the normal new operator failing?<br>&nbsp;a) This is left up to the implementation to decide <br>b) It returns NULL <br>c) It throws an exception <br><br>Question #9 What is a difficulty that arises with handling out of memory errors? <br>a) Many cleanup operations require extra memory <br>b) Running out of memory rarely happens in systems that interact with users <br>c) Once you've run out of memory, your program cannot continue<br><br>&nbsp;Question #10 What is the result of this code? myObj *foo = operator new(sizeof(foo)); <br>a) That's not legal syntax! <br>b) foo has memory allocated for it, but is not constructed <br>c) foo has memory allocated for it, and is fully constructed </span>
<img src ="http://www.cppblog.com/flyonok/aggbug/115658.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyonok/" target="_blank">flyonok</a> 2010-05-18 11:54 <a href="http://www.cppblog.com/flyonok/archive/2010/05/18/115658.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>