⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 [c++对象模型][4]指针与字符串 - itech's blog - 博客园.htm

📁 最为全面的c++内存模型探秘。不管是初学者还是老手
💻 HTM
📖 第 1 页 / 共 5 页
字号:
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> strlen(c3); 
</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 
3</SPAN><SPAN style="COLOR: #008000"> <BR></SPAN><SPAN 
style="COLOR: #000000">&nbsp;&nbsp;&nbsp; free(c3); </SPAN><SPAN 
style="COLOR: #008000">//</SPAN><SPAN 
style="COLOR: #008000">如果这里不free,会内存泄漏</SPAN><SPAN style="COLOR: #008000"> 
<BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp; c3 </SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">abc</SPAN><SPAN 
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">; </SPAN><SPAN 
style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> abc\0 
在常量区,c3指向了常量区 <BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN 
style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">c3[1] = 'g'; 
</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 
常量不能修改</SPAN><SPAN style="COLOR: #008000"> <BR></SPAN><SPAN 
style="COLOR: #000000">&nbsp;&nbsp;&nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> y </SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> strlen(c3); 
</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 
3</SPAN><SPAN style="COLOR: #008000"> <BR></SPAN><SPAN 
style="COLOR: #000000">}</SPAN></SPAN></DIV>
<P>&nbsp;</P>
<P><SPAN style="COLOR: #ff0000">字符串都以\0结尾,所以例如:<SPAN 
style="COLOR: #ff0000"><SPAN id=Code_Open_Text_145503><SPAN 
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #ff0000">char<SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">c1 </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">abc</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">;</SPAN></SPAN></SPAN></SPAN><SPAN 
style="COLOR: #ff0000"><SPAN id=Code_Open_Text_145503><SPAN 
style="COLOR: #ff0000"><SPAN style="COLOR: #0000ff">char</SPAN><SPAN 
style="COLOR: #000000"> c2[] </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">abc</SPAN><SPAN 
style="COLOR: #800000">"</SPAN></SPAN><SPAN style="COLOR: #000000"><SPAN 
style="COLOR: #ff0000">;,</SPAN><SPAN 
style="COLOR: #ff0000">使用strlen得到长度都为3,但是实际的存储空间为strlen+1即3+1。</SPAN></SPAN></SPAN></SPAN></SPAN></P>
<P>二 C中字符串操作函数</P>
<P>C++的程序员对C中的字符串指针操作的函数却并不是相当的熟悉。而C中的这些字符串的指针操作函数有的时候也是必须要面对的,比如我们的库要提供C函数接口,保持向后兼容和跨平台,还有我们经常使用一些第三方的库中都或多或少的使用到了这些C中的指针操作函数,所以下面列出C的指针操作函数,帮助大家熟悉之。</P>
<P>1) memcpy/memset/memcmp</P>
<TABLE cellSpacing=0 cellPadding=2 width="80%" border=2>
  <TBODY>
  <TR>
    <TD vAlign=top width=99>
      <P>&nbsp;</P>
      <P>&nbsp;&nbsp;&nbsp; memcpy</P></TD>
    <TD vAlign=top width=454>
      <P>原型:extern void *memcpy( void *to, const void *from, size_t count ); 
      <BR>包含:#include &lt;string.h&gt; 或&lt;string&gt;或&lt;cstring&gt; 
      <BR>功能:由src所指内存区域复制count个字节到dest所指内存区域。 
      <BR>说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。</P></TD></TR>
  <TR>
    <TD vAlign=top width=104>
      <P>&nbsp;</P>
      <P>&nbsp;&nbsp;&nbsp; memset</P></TD>
    <TD vAlign=top width=454>
      <P>原型:extern void* memset( void* buffer, int ch, size_t count ); 
      <BR>包含:#include &lt;string.h&gt; 或&lt;string&gt;或&lt;cstring&gt; 
      <BR>功能:把buffer所指内存区域的前count个字节设置成字符c。 <BR>说明:返回指向buffer的指针。</P></TD></TR>
  <TR>
    <TD vAlign=top width=108>
      <P>&nbsp;</P>
      <P>&nbsp;</P>
      <P>&nbsp;&nbsp; memcmp</P></TD>
    <TD vAlign=top width=454>
      <P>原型:extern int memcmp(const void *buffer1, const void *buffer2, size_t 
      count ); <BR>包含:#include &lt;string.h&gt; 或&lt;string&gt;或&lt;cstring&gt; 
      <BR>功能:比较内存区域buf1和buf2的前count个字节。 <BR>说明: 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当buf1&lt;buf2时,返回值&lt;0 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当buf1=buf2时,返回值=0 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    当buf1&gt;buf2时,返回值&gt;0</P></TD></TR>
  <TR>
    <TD vAlign=top width=112><BR><BR>&nbsp;&nbsp;&nbsp; memchr <BR></TD>
    <TD vAlign=top width=454>原型: extern void *memchr( const void *buffer, int 
      ch, size_t count ); <BR>包含:#include &lt;string.h&gt; 
      或&lt;string&gt;或&lt;cstring&gt; <BR>功能:查找ch在buffer中第一次出现的位置。 
      <BR>说明:如果发现返回指针,如果没有返回NULL。 <BR></TD></TR></TBODY></TABLE>
<P>&nbsp;</P>
<P>实例:</P>
<P>&nbsp;</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_154053 style="DISPLAY: none" 
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_154053').style.display='none'; document.getElementById('Code_Open_Image_154053').style.display='inline'; document.getElementById('Code_Open_Text_154053').style.display='inline';" 
height=16 
src="[C++对象模型][4]指针与字符串 - iTech's Blog - 博客园.files/ContractedBlock.gif" width=11 
align=top><IMG id=Code_Open_Image_154053 
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_154053').style.display='none'; getElementById('Code_Closed_Image_154053').style.display='inline'; getElementById('Code_Closed_Text_154053').style.display='inline';" 
height=16 
src="[C++对象模型][4]指针与字符串 - iTech's Blog - 博客园.files/ExpandedBlockStart.gif" 
width=11 align=top><SPAN class=cnblogs_code_Collapse 
id=Code_Closed_Text_154053>Code</SPAN><SPAN id=Code_Open_Text_154053> <BR><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000"> 
TestMemFunction() <BR>{ <BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">char</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">s1</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">Hello!</SPAN><SPAN 
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">; </SPAN><SPAN 
style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 
Hello!\0</SPAN><SPAN style="COLOR: #008000"> <BR></SPAN><SPAN 
style="COLOR: #000000">&nbsp;&nbsp;&nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> l </SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> strlen(s1); 
</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 6 
</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN 
style="COLOR: #000000">&nbsp;&nbsp;&nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">char</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">d1 </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">[l</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN 
style="COLOR: #000000">]; </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN 
style="COLOR: #008000"> d1 需要strlen(s1) + 1 空间</SPAN><SPAN 
style="COLOR: #008000"> <BR></SPAN><SPAN 
style="COLOR: #000000">&nbsp;&nbsp;&nbsp; memcpy(d1,s1,l</SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN 
style="COLOR: #000000">); <BR><BR>&nbsp;&nbsp;&nbsp; memcpy(d1,d1,l); 
<BR><BR>&nbsp;&nbsp;&nbsp; memmove(d1 </SPAN><SPAN 
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">,d1,l</SPAN><SPAN 
style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN 
style="COLOR: #000000">); <BR><BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">const</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN 
style="COLOR: #000000"> ARRAY_LENGTH </SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800080">5</SPAN><SPAN style="COLOR: #000000">; 
<BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN 
style="COLOR: #000000"> the_array[ARRAY_LENGTH]; <BR>&nbsp;&nbsp;&nbsp; 
</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> zero 
out the contents of the_array</SPAN><SPAN style="COLOR: #008000"> 
<BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp; memset( the_array, 
</SPAN><SPAN style="COLOR: #800000">'</SPAN><SPAN 
style="COLOR: #800000">c</SPAN><SPAN style="COLOR: #800000">'</SPAN><SPAN 
style="COLOR: #000000">, ARRAY_LENGTH ); <BR><BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">char</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">a1 </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">source1</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">; <BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000"> 
arr1[</SPAN><SPAN style="COLOR: #800080">8</SPAN><SPAN style="COLOR: #000000">] 
</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">source2</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">; <BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> r </SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> 
memcmp(a1,arr1,strlen(a1) </SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN 
style="COLOR: #000000">); </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN 
style="COLOR: #008000"> 仅比较source,所以相等</SPAN><SPAN style="COLOR: #008000"> 
<BR></SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000"> str[</SPAN><SPAN 
style="COLOR: #800080">17</SPAN><SPAN style="COLOR: #000000">]; 
<BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">ptr; <BR>&nbsp;&nbsp;&nbsp; strcpy(str, </SPAN><SPAN 
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">This is a 
string</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">); <BR>&nbsp;&nbsp;&nbsp; ptr </SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (</SPAN><SPAN 
style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">)memchr(str, </SPAN><SPAN 
style="COLOR: #800000">'</SPAN><SPAN style="COLOR: #800000">r</SPAN><SPAN 
style="COLOR: #800000">'</SPAN><SPAN style="COLOR: #000000">, strlen(str)); 
<BR>}</SPAN></SPAN></DIV>
<P>&nbsp;</P>
<P>2) strlen/strcpy/strcat/strcmp/strchr/strcoll/strstr/strtok/strtod/strtol</P>
<TABLE cellSpacing=0 cellPadding=2 width=400 border=2>
  <TBODY>
  <TR>
    <TD vAlign=top width=81><BR>strcpy</TD>
    <TD vAlign=top width=319>
      <P>char *strcpy(char *s1, const char *s2) 
  将字符串s2复制到字符串数组s1中,返回s1的值</P></TD></TR>
  <TR>
    <TD vAlign=top width=81><BR>strcat</TD>
    <TD vAlign=top width=319>
      <P>char *strcat(char *s1, const char *s2) 
      <BR>将字符串s2添加到字符串s1的后面。s2的第一个字符重定义s1的null终止符。返回s1的值</P></TD></TR>
  <TR>
    <TD vAlign=top width=81><BR><BR>strcmp</TD>
    <TD vAlign=top width=319>
      <P>int strcmp(const char *s1, const char *s2) 
      <BR>比较字符串s1和字符串s2。函数在s1等于、小于或大于s2时分别返回0、小于0或者大于0的值</P></TD></TR>
  <TR>
    <TD vAlign=top width=81>strchr</TD>
    <TD vAlign=top width=319>
      <P>char *strchr(char * str,int c ); 在str中查找c第一次出现的位置。</P></TD></TR>
  <TR>
    <TD vAlign=top width=81>strstr</TD>
    <TD vAlign=top width=319>char *strstr(char *str,const char *strSearch 
      );在string1中查找string2第一次出现的位置。</TD></TR>
  <TR>
    <TD vAlign=top width=81>strtok</TD>
    <TD vAlign=top width=319>char *strtok(char *strToken,const char 
      *strDelimit ); 分割字符串。</TD></TR></TBODY></TABLE>
<P>&nbsp;</P>
<P>实例:</P>
<P>&nbsp;</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_163224 style="DISPLAY: none" 
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_163224').style.display='none'; document.getElementById('Code_Open_Image_163224').style.display='inline'; document.getElementById('Code_Open_Text_163224').style.display='inline';" 
height=16 
src="[C++对象模型][4]指针与字符串 - iTech's Blog - 博客园.files/ContractedBlock.gif" width=11 
align=top><IMG id=Code_Open_Image_163224 
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_163224').style.display='none'; getElementById('Code_Closed_Image_163224').style.display='inline'; getElementById('Code_Closed_Text_163224').style.display='inline';" 
height=16 
src="[C++对象模型][4]指针与字符串 - iTech's Blog - 博客园.files/ExpandedBlockStart.gif" 
width=11 align=top><SPAN class=cnblogs_code_Collapse 
id=Code_Closed_Text_163224>Code</SPAN><SPAN id=Code_Open_Text_163224><BR><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;TestStrFunction()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">char</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN 
style="COLOR: #800080">11</SPAN><SPAN 
style="COLOR: #000000">];<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">char</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">str1&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">123456789</SPAN><SPAN 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -