﻿<?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++博客-天涯浪子-文章分类-（三）C++高级部分——Unix C++部分</title><link>http://www.cppblog.com/junfeng568/category/1072.html</link><description>==&gt;&gt;多情总被无情伤，此生无情又何妨！ 红豆本是相思子，一寸相思一寸灰。此生不能续旧盟，必当续之于来生。</description><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 18:48:46 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 18:48:46 GMT</pubDate><ttl>60</ttl><item><title> 如何查看系统变量</title><link>http://www.cppblog.com/junfeng568/articles/4327.html</link><dc:creator>天涯浪子</dc:creator><author>天涯浪子</author><pubDate>Sat, 18 Mar 2006 14:01:00 GMT</pubDate><guid>http://www.cppblog.com/junfeng568/articles/4327.html</guid><wfw:comment>http://www.cppblog.com/junfeng568/comments/4327.html</wfw:comment><comments>http://www.cppblog.com/junfeng568/articles/4327.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/junfeng568/comments/commentRss/4327.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/junfeng568/services/trackbacks/4327.html</trackback:ping><description><![CDATA[<p> //该程序用来查看自己的系统环境,相当于在命令行输入env所显示的结果.<br />#include &lt;iostream&gt;<br />using namespace std;</p>
		<p>int main( int argc , char* argv[] , char* env[] ){//也可写成int main(int argc,char* *argv,char* *env[]){<br />  //int argc表示命令行参数的个数,<br />  //char* argv[]表示命令行参数的值<br /> cout&lt;&lt;"begin ..." &lt;&lt; endl;<br /> while( env!=NULL &amp;&amp; *env != NULL ){// 二者是等价的while(env&amp;&amp;*env){<br />  cout&lt;&lt; *env &lt;&lt; endl;<br />  env++;<br /> }<br /> cout&lt;&lt;"end  ...." &lt;&lt; endl;<br /> return 0 ;<br />} </p><img src ="http://www.cppblog.com/junfeng568/aggbug/4327.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/junfeng568/" target="_blank">天涯浪子</a> 2006-03-18 22:01 <a href="http://www.cppblog.com/junfeng568/articles/4327.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> 如何取得环境变量</title><link>http://www.cppblog.com/junfeng568/articles/4325.html</link><dc:creator>天涯浪子</dc:creator><author>天涯浪子</author><pubDate>Sat, 18 Mar 2006 14:00:00 GMT</pubDate><guid>http://www.cppblog.com/junfeng568/articles/4325.html</guid><wfw:comment>http://www.cppblog.com/junfeng568/comments/4325.html</wfw:comment><comments>http://www.cppblog.com/junfeng568/articles/4325.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/junfeng568/comments/commentRss/4325.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/junfeng568/services/trackbacks/4325.html</trackback:ping><description><![CDATA[<p> /*变量p用来存放自己定义的环境变量,而pt用来存放系统环境变量的PATH值<br />注意:此例应该先判断变量的返回值是否为空<br />取得环境变量函数的格式如下:<br /> char* getenv(const char *name); return value for environment name.<br /> <br /> int putenv(const char* str); change or add value to environment.<br />*/<br />#include &lt;stdlib.h&gt;<br />#include &lt;iostream&gt;<br />using namespace std;</p>
		<p>int main(){<br /> char *p = NULL;<br /> char *pt=NULL;<br /> <br /> p = getenv( "AAA" );<br /> pt =getenv("PATH");<br /> <br /> if( p == NULL )<br />  cout&lt;&lt;"No such env named AAA"&lt;&lt;endl;<br /> else<br />  cout&lt;&lt;"AAA=" &lt;&lt; p &lt;&lt; endl;<br /> if( pt == NULL )<br />  cout&lt;&lt;"No such env named PATH"&lt;&lt;endl;<br /> else<br />  cout&lt;&lt;"PATH=" &lt;&lt; pt&lt;&lt; endl;<br /> putenv( "AAA=123456" );<br /> cout&lt;&lt;"AAA=" &lt;&lt; getenv( "AAA" ) &lt;&lt; endl;<br /> putenv("AAA=123");<br /> cout&lt;&lt;"AAA="&lt;&lt;getenv("AAA")&lt;&lt;endl;<br /> <br /> cout&lt;&lt;"PATH="&lt;&lt;getenv("PATH")&lt;&lt;endl;<br /> return 0 ;<br />}</p>
		<p>/*问题:<br /> 假如环境变量YU在用户的shell下的值是123456,在我们的应用程序中<br /> 通过getenv()和putenv()来更改该环境变量为abcdef,那么当我们结束应用<br /> 程序后回到shell下时,我们用命令echo $YU,那么结果应该是什么?<br />*/</p><img src ="http://www.cppblog.com/junfeng568/aggbug/4325.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/junfeng568/" target="_blank">天涯浪子</a> 2006-03-18 22:00 <a href="http://www.cppblog.com/junfeng568/articles/4325.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> 高级进程通信技术------IPC通信</title><link>http://www.cppblog.com/junfeng568/articles/4152.html</link><dc:creator>天涯浪子</dc:creator><author>天涯浪子</author><pubDate>Tue, 14 Mar 2006 12:50:00 GMT</pubDate><guid>http://www.cppblog.com/junfeng568/articles/4152.html</guid><wfw:comment>http://www.cppblog.com/junfeng568/comments/4152.html</wfw:comment><comments>http://www.cppblog.com/junfeng568/articles/4152.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/junfeng568/comments/commentRss/4152.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/junfeng568/services/trackbacks/4152.html</trackback:ping><description><![CDATA[<P><BR>一。有关的系统命令<BR>&nbsp;//以下是在unix下进行的操作<BR>&nbsp;1。ipcs<BR>&nbsp;执行结果如下：<BR>&nbsp;IPC status from &lt;running system&gt; as of 2006年03月13日 星期一 16时38分56秒 CST<BR>T&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; KEY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MODE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OWNER&nbsp;&nbsp;&nbsp; GROUP<BR>Message Queues:<BR>Shared Memory:<BR>m&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp; 0x50000d2b --rw-r--r--&nbsp;&nbsp;&nbsp;&nbsp; root&nbsp;&nbsp;&nbsp;&nbsp; root<BR>Semaphores:<BR>s&nbsp;&nbsp;&nbsp;&nbsp; 131075&nbsp;&nbsp; 0x123455&nbsp;&nbsp; --ra-------&nbsp; yuqiang&nbsp;&nbsp;&nbsp; staff</P>
<P>&nbsp;2.ipcrm -q/m/s&nbsp; ID号<BR>&nbsp;作用 ：remove a message queue ,semaphore set ,or shared memory ID.<BR>&nbsp;执行命令ipcrm -s 131075后，结果如下：<BR>&nbsp;IPC status from &lt;running system&gt; as of 2006年03月13日 星期一 16时36分26秒 CST<BR>T&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; KEY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MODE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OWNER&nbsp;&nbsp;&nbsp; GROUP<BR>Message Queues:<BR>Shared Memory:<BR>m&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp; 0x50000d2b --rw-r--r--&nbsp;&nbsp;&nbsp;&nbsp; root&nbsp;&nbsp;&nbsp;&nbsp; root<BR>Semaphores:</P><img src ="http://www.cppblog.com/junfeng568/aggbug/4152.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/junfeng568/" target="_blank">天涯浪子</a> 2006-03-14 20:50 <a href="http://www.cppblog.com/junfeng568/articles/4152.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> memset ,memcpy 和strcpy 的根本区别？</title><link>http://www.cppblog.com/junfeng568/articles/4023.html</link><dc:creator>天涯浪子</dc:creator><author>天涯浪子</author><pubDate>Sat, 11 Mar 2006 14:01:00 GMT</pubDate><guid>http://www.cppblog.com/junfeng568/articles/4023.html</guid><wfw:comment>http://www.cppblog.com/junfeng568/comments/4023.html</wfw:comment><comments>http://www.cppblog.com/junfeng568/articles/4023.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/junfeng568/comments/commentRss/4023.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/junfeng568/services/trackbacks/4023.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 27.95pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd; mso-char-indent-count: 1.99" align=left><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memset<SPAN style="mso-spacerun: yes">&nbsp; </SPAN></SPAN></B><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">用来对一段内存空间全部设置为某个字符，一般用在对定义的字符串进行初始化为</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">‘ ’</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">或</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">‘\<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:chmetcnv w:st="on" UnitName="’" SourceValue="0" HasSpace="False" Negative="False" NumberType="1" TCSC="0">0’</st1:chmetcnv></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">；</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 27pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd; mso-char-indent-count: 2.25" align=left><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">例</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">:char a[100];memset(a, '\0', sizeof(a));<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>memset</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">可以方便的清空一个结构类型的变量或数组。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-char-indent-count: 2.0" align=left><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">如：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 53.75pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 5.12gd" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">struct sample_struct<BR>{<BR>&nbsp;char &nbsp; csName[16];<BR>&nbsp;int &nbsp; &nbsp;iSeq;<BR>&nbsp;int &nbsp; &nbsp;iType;<BR>};<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd" align=left><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">对于变量</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><BR>struct sample_strcut &nbsp;stTest; <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd" align=left><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">一般情况下，清空</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">stTest</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">的方法：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">stTest.csName[0]='\0';<BR>stTest.iSeq=0;<BR>stTest.iType=0; <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd" align=left><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">memset</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">就非常方便：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><BR>memset(&amp;stTest,0,sizeof(struct sample_struct));<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd" align=left><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">如果是数组：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>struct sample_struct &nbsp; TEST[10];<BR></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">则</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><BR>memset(TEST,0,sizeof(struct sample_struct)*10);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 35pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd; mso-char-indent-count: 2.49" align=left><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">memcpy<SPAN style="mso-spacerun: yes">&nbsp; </SPAN></SPAN></B><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">用来做内存拷贝，你可以拿它拷贝任何数据类型的对象，可以指定拷贝的数据长度。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 23.9pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd; mso-char-indent-count: 1.99" align=left><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">例：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">char a[100],b[50]; memcpy(b, a, sizeof(b));</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">注意如用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">sizeof(a)</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">，会造成</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">b</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">的内存地址溢出。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 56.2pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-char-indent-count: 4.0" align=left><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">Strcpy<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN></SPAN></B><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">就只能拷贝字符串了，它遇到</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">'\0'</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">就结束拷贝。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 33pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd; mso-char-indent-count: 2.75" align=left><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">例：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">char a[100],b[50];strcpy(a,b);</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">如用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">strcpy(b,a)</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">，要注意</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">a</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">中的字符串长度（第一个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">‘\<st1:chmetcnv w:st="on" UnitName="’" SourceValue="0" HasSpace="False" Negative="False" NumberType="1" TCSC="0">0’</st1:chmetcnv></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">之前）是否超过</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">50</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">位，如超过，则会造成</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt">b</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-font-kerning: 0pt; mso-ascii-font-family: 'Times New Roman'">的内存地址溢出。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 36pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd; mso-char-indent-count: 3.0" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312">str</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-ascii-font-family: 'Times New Roman'">也可以用用个参数的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312">strncpy(a,b,n)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312">========================================================<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312">memset</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-ascii-font-family: 'Times New Roman'">主要应用是初始化某个内存空间。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312"><BR>memcpy</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-ascii-font-family: 'Times New Roman'">是用于</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312">copy</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-ascii-font-family: 'Times New Roman'">源空间的数据到目的空间中。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312"><BR>strcpy</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-ascii-font-family: 'Times New Roman'">用于字符串</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312">copy,</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-ascii-font-family: 'Times New Roman'">遇到</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312">‘\<st1:chmetcnv w:st="on" UnitName="’" SourceValue="0" HasSpace="False" Negative="False" NumberType="1" TCSC="0">0’</st1:chmetcnv></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-ascii-font-family: 'Times New Roman'">，将结束。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-para-margin-left: 2.0gd" align=left><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-ascii-font-family: 'Times New Roman'">如果你理解了这些，你应该知道他们的区别：例如你初始化某块空间的时候，用到</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312">memcpy</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-ascii-font-family: 'Times New Roman'">，那么应该怎么写，是不是显得很笨。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312"><BR><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>int &nbsp;m[100]<BR>-&gt;memset((void*)m,0x00,sizeof(int)*100);//Ok</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 仿宋_GB2312; mso-ascii-font-family: 'Times New Roman'">！</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; mso-fareast-font-family: 仿宋_GB2312"><BR>…memcpy((void*)m,"\0\0\0\0....",sizeof(int)*100);//it’s wrong.<o:p></o:p></SPAN></P><img src ="http://www.cppblog.com/junfeng568/aggbug/4023.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/junfeng568/" target="_blank">天涯浪子</a> 2006-03-11 22:01 <a href="http://www.cppblog.com/junfeng568/articles/4023.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>