{"id":240,"date":"2007-02-03T21:21:00","date_gmt":"2007-02-03T21:21:00","guid":{"rendered":"http:\/\/marvelliu.wordpress.com\/2007\/02\/03\/char%e5%92%8cchar"},"modified":"2007-02-03T21:21:00","modified_gmt":"2007-02-03T21:21:00","slug":"char%e5%92%8cchar","status":"publish","type":"post","link":"https:\/\/marvelliu.space\/?p=240","title":{"rendered":"char*\u548cchar[]"},"content":{"rendered":"<p>\u4e00\u76f4\u5c31\u8ba4\u4e3aC\u4e2d\u7684char*\u548cchar\u51e0\u4e4e\u662f\u4e00\u6837\u7684\uff0c\u8981\u662f\u8bf4\u533a\u522b\uff0c\u90a3\u5c31\u662fchar[]\u5728\u58f0\u660e\u4e4b\u540e\uff0c\u5730\u5740\u503c\u662f\u53ea\u8bfb\u7684\uff0c\u800cchar*\u7684\u503c\u662f\u53ef\u53d8\u7684\u3002<\/p>\n<p>\u4e0d\u8fc7\u4eca\u5929\u770b\u5230\u4e24\u4e2a\u7a0b\u5e8f\uff1a<\/p>\n<p>A\uff1a<\/p>\n<p>char* get_str(void)<br \/>{<br \/>char str[] = {&#8220;abcdefghijk&#8221;};<br \/>return str;<br \/>}<br \/>int main(int argc, char* argv[])<br \/>{<br \/>char* p = get_str();<br \/>printf(&#8220;%sn&#8221;,p);<br \/>return 0;<br \/>}<\/p>\n<p>B\uff1a<\/p>\n<p>char* get_str(void)<br \/>{<br \/>char *str = {&#8220;abcdefghijk&#8221;};<br \/>return str;<br \/>}<br \/>int main(int argc, char* argv[])<br \/>{<br \/>char* p = get_str();<br \/>printf(&#8220;%sn&#8221;,p);<br \/>return 0;<br \/>}<\/p>\n<p>\u7406\u8bba\u4e0a\uff0c\u4e24\u4e2a\u7a0b\u5e8f\u597d\u50cf\u90fd\u4e0d\u53ef\u4ee5\uff0c\u56e0\u4e3a\u90fd\u662f\u8fd4\u56de\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u3002\u53ef\u7ed3\u679c\u662f\uff0c\u7b2c\u4e00\u4e2a\u6709warning\uff0c\u8fd0\u884c\u7684\u786e\u4ec0\u4e48\u90fd\u6ca1\u6709\u6253\u5370\u51fa\u6765\uff1b\u800c\u7b2c\u4e8c\u4e2a\u4e00\u5207\u6b63\u5e38\uff0c\u80fd\u6253\u5370\u51fa\u5b57\u6bcd\u6765\u3002\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f\u6211\u7684gcc\u7248\u672c\u662f version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)<\/p>\n<p>\u5c06c\u4ee3\u7801\u8f6c\u5316\u6210\u6c47\u7f16\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/marvel.hit.edu.cn:8080\/wp-content\/uploads\/2007\/02\/gcc-local-varibles.png\" title=\"gcc-local-varibles.png\" alt=\"gcc-local-varibles.png\" height=\"900\" width=\"1261\" \/><br \/>\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u4e24\u4e2a\u7a0b\u5e8f\u53ea\u6709\u5728get_str \u51fd\u6570\u6709\u533a\u522b\uff0c\u524d\u8005\u662f\u5c06.LC0\uff0c\u5373\u5b57\u7b26\u4e32\u91cd\u65b0\u590d\u5236\u4e86\u4e00\u904d\uff0c\u8fdb\u884c\u4fee\u6539\uff1b\u800c\u540e\u8005\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u7684\u5730\u5740\u8d4b\u7ed9\u4e86\u5c40\u90e8\u53d8\u91cf\u3002movl    -4(%ebp), %eax \u5219\u662f\u5c06\u8fd9\u4e2a\u5730\u5740\u8fd4\u56de\u7ed9main\u3002<\/p>\n<p>\u770b\u6765gcc\u7f16\u8bd1\u5668\u4e0d\u662f\u50cf\u6211\u4eec\u60f3\u8c61\u7684\u90a3\u6837\uff0c\u5728\u51fd\u6570\u4e2d\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5728\u8fdb\u884c\u64cd\u4f5c\uff1b\u800c\u662f\u6709\u4e00\u4e2a\u5168\u5c40\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u662fchar[]\uff0c\u5219\u4f1a\u751f\u6210\u4e00\u4e2a\u526f\u672c\uff0c\u5982\u679c\u662fchar*\uff0c\u5219\u53ea\u662f\u7b80\u5355\u7684\u751f\u6210\u4e00\u4e2a\u5f15\u7528\u3002<\/p>\n<p>\u53ef\u662f\u95ee\u9898\u6ca1\u6709\u7ed3\u675f\uff0c\u5173\u952e\u5728\u4e8e\u8fd9\u4e2a\u5b57\u7b26\u4e32\u662f\u5728 .section    .rodata\u4e0b\u9762\u7684\uff0c\u4e5f\u5c31\u662f\u53ea\u8bfb\u7684\uff0c\u6709\u610f\u601d\u7684\u662f\u5982\u679c\u6211\u4eec\u5c06B\u4e2d\u7684get_str\u6539\u6210\uff1a<\/p>\n<p>char* get_str(void)<br \/>{<br \/>char *str = {&#8220;abcdefghijk&#8221;};<br \/>str[1]=&#8217;p&#8217;;<br \/>return str;<br \/>}<\/p>\n<p>\u4e4b\u540e\uff0c\u8fd0\u884c\u4f1a\u51fa\u73b0 Segmentation fault\uff0c\u800c\u8fdb\u884c\u76f8\u540c\u4fee\u6539\u7684A\u4ee3\u7801\u53ef\u4ee5\u8fd0\u884c\uff0c\u4f46\u662f\u6253\u5370\u51fa\u6765\u7684\u662f\u65e0\u610f\u4e49\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u89e3\u51b3\u4e86\uff0c\u8fd8\u6709\u4e00\u4e9b\u5f88\u6709\u610f\u601d\u7684\u4e1c\u897f\u3002gcc \u73b0\u5728\u7684\u7248\u672c\u4e3a\u4e86\u52a0\u5feb\u8fd0\u884c\u901f\u5ea6\uff0c\u90fd\u5c06\u53d8\u91cf\u6309\u716716\u5b57\u8282\u8fb9\u754c\u5b58\u50a8\u3002\u4f8b\u5982\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<p>void doit(void)<br \/>{<br \/>}<br \/>int main(int argc, char* argv[])<br \/>{<br \/>char a[10];<br \/>doit();<br \/>return 0;<br \/>}<\/p>\n<p>\u751f\u6210\u7684\u6c47\u7f16\u4ee3\u7801main\u90e8\u5206\u4e3a\uff1a<\/p>\n<p>main:<br \/>leal    4(%esp), %ecx<br \/>andl    $-16, %esp<br \/>pushl   -4(%ecx)<br \/>pushl   %ebp<br \/>movl    %esp, %ebp<br \/>pushl   %ecx<br \/>subl    $16, %esp<br \/>call    doit<br \/>addl    $16, %esp<br \/>popl    %ecx<br \/>popl    %ebp<br \/>leal    -4(%ecx), %esp<br \/>ret<br \/>\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0main\u51fd\u6570\u5728\u521d\u59cb\u5316\u7684\u65f6\u5019 \u9884\u7559\u4e8616\u4e2a\u5b57\u8282\u7684\u7a7a\u95f4\u7ed9\u5c40\u90e8\u53d8\u91cf\u3002\u5982\u679c\u6211\u4eec\u5728main\u4e2d\u518d\u6dfb\u52a0\u4e00\u4e2achar b\uff0c\u7ed3\u679c\u8fd8\u662f16\u4e2a\u5b57\u8282\uff0c\u56e0\u4e3agcc\u4f1a\u5c06\u6240\u6709\u53d8\u91cf\u603b\u5171\u9700\u8981\u7684\u7a7a\u95f4\u7b97\u51fa\u6765\uff0c\u518d\u8f6c\u5316\u4e3a16\u7684\u500d\u6570\u3002\u662f\u4e0d\u662f\u6240\u6709\u7684\u60c5\u51b5\u90fd\u662f\u8fd9\u6837\u5462\uff1f\u770b\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<p>void doit(int n)<br \/>{<br \/>}<br \/>int main(int argc, char* argv[])<br \/>{<br \/>char a[120];<br \/>doit(9);<br \/>return 0;<br \/>}<br \/>\u8fd9\u4e2a\u65f6\u5019\u76f8\u5173\u6c47\u7f16\u4ee3\u7801\u53d8\u6210\u4e86\uff1a<\/p>\n<p>subl    $132, %esp<br \/>movl    $9, (%esp)<br \/>call    doit<br \/>movl    $0, %eax<br \/>addl    $132, %esp<br \/>\u4e3a\u4ec0\u4e48\u662f132\u800c\u4e0d\u662f128\u5462\uff1f\u56e0\u4e3amain\u51fd\u6570\u8c03\u7528get_str\u7684\u65f6\u5019\u9700\u8981\u9884\u5148\u586b\u5165\u5176\u6240\u9700\u7684\u53c2\u6570\uff0c\u7136\u540e\u624d\u6267\u884ccall\u586b\u5165IP\uff0c\u6240\u4ee5gcc\u5c31\u5077\u61d2\u4e00\u8d77\u7559\u4e86\uff0c\u56e0\u4e3a    \u53c2\u6570\u662f\u4e00\u4e2aint\uff0c\u4e3a4\u4e2a\u5b57\u8282\uff0c\u6240\u4ee5\u603b\u5171\u9700\u8981\u7559\u51fa128+4=132\u4e2a\u5b57\u8282\u3002<\/p>\n<p>\u5177\u4f53\u7684\u5185\u5b58\u5206\u5e03\u56fe\u53c2\u89c1\u4e0b\u56fe<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-overflow\/images\/image004.gif\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u76f4\u5c31\u8ba4\u4e3aC\u4e2d\u7684char*\u548cchar\u51e0\u4e4e\u662f\u4e00\u6837\u7684\uff0c\u8981\u662f\u8bf4\u533a\u522b\uff0c\u90a3\u5c31\u662fchar[]\u5728\u58f0\u660e\u4e4b\u540e\uff0c\u5730\u5740\u503c\u662f\u53ea\u8bfb\u7684\uff0c\u800cchar*\u7684\u503c\u662f\u53ef\u53d8\u7684\u3002 \u4e0d\u8fc7\u4eca\u5929\u770b\u5230\u4e24\u4e2a\u7a0b\u5e8f\uff1a A\uff1a char* get_str(void){char str[] = {&#8220;abcdefghijk&#8221;};return str;}int main(int argc, char* argv[]){char* p = get_str();printf(&#8220;%sn&#8221;,p);return 0;} B\uff1a char* get_str(void){char *str = {&#8220;abcdefghijk&#8221;};return str;}int main(int argc, char* argv[]){char* p = get_str();printf(&#8220;%sn&#8221;,p);return 0;} \u7406\u8bba\u4e0a\uff0c\u4e24\u4e2a\u7a0b\u5e8f\u597d\u50cf\u90fd\u4e0d\u53ef\u4ee5\uff0c\u56e0\u4e3a\u90fd\u662f\u8fd4\u56de\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u3002\u53ef\u7ed3\u679c\u662f\uff0c\u7b2c\u4e00\u4e2a\u6709warning\uff0c\u8fd0\u884c\u7684\u786e\u4ec0\u4e48\u90fd\u6ca1\u6709\u6253\u5370\u51fa\u6765\uff1b\u800c\u7b2c\u4e8c\u4e2a\u4e00\u5207\u6b63\u5e38\uff0c\u80fd\u6253\u5370\u51fa\u5b57\u6bcd\u6765\u3002\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f\u6211\u7684gcc\u7248\u672c\u662f version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21) \u5c06c\u4ee3\u7801\u8f6c\u5316\u6210\u6c47\u7f16\uff1a \u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u4e24\u4e2a\u7a0b\u5e8f\u53ea\u6709\u5728get_str \u51fd\u6570\u6709\u533a\u522b\uff0c\u524d\u8005\u662f\u5c06.LC0\uff0c\u5373\u5b57\u7b26\u4e32\u91cd\u65b0\u590d\u5236\u4e86\u4e00\u904d\uff0c\u8fdb\u884c\u4fee\u6539\uff1b\u800c\u540e\u8005\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u7684\u5730\u5740\u8d4b\u7ed9\u4e86\u5c40\u90e8\u53d8\u91cf\u3002movl -4(%ebp), %eax \u5219\u662f\u5c06\u8fd9\u4e2a\u5730\u5740\u8fd4\u56de\u7ed9main\u3002 \u770b\u6765gcc\u7f16\u8bd1\u5668\u4e0d\u662f\u50cf\u6211\u4eec\u60f3\u8c61\u7684\u90a3\u6837\uff0c\u5728\u51fd\u6570\u4e2d\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5728\u8fdb\u884c\u64cd\u4f5c\uff1b\u800c\u662f\u6709\u4e00\u4e2a\u5168\u5c40\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u662fchar[]\uff0c\u5219\u4f1a\u751f\u6210\u4e00\u4e2a\u526f\u672c\uff0c\u5982\u679c\u662fchar*\uff0c\u5219\u53ea\u662f\u7b80\u5355\u7684\u751f\u6210\u4e00\u4e2a\u5f15\u7528\u3002 \u53ef\u662f\u95ee\u9898\u6ca1\u6709\u7ed3\u675f\uff0c\u5173\u952e\u5728\u4e8e\u8fd9\u4e2a\u5b57\u7b26\u4e32\u662f\u5728 .section .rodata\u4e0b\u9762\u7684\uff0c\u4e5f\u5c31\u662f\u53ea\u8bfb\u7684\uff0c\u6709\u610f\u601d\u7684\u662f\u5982\u679c\u6211\u4eec\u5c06B\u4e2d\u7684get_str\u6539\u6210\uff1a char* get_str(void){char *str = {&#8220;abcdefghijk&#8221;};str[1]=&#8217;p&#8217;;return str;} &hellip; <a href=\"https:\/\/marvelliu.space\/?p=240\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;char*\u548cchar[]&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-240","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts\/240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=240"}],"version-history":[{"count":0,"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions"}],"wp:attachment":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}