{"id":3449,"date":"2025-04-06T00:05:12","date_gmt":"2025-04-05T16:05:12","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3449"},"modified":"2025-04-06T00:05:12","modified_gmt":"2025-04-05T16:05:12","slug":"c-%e8%af%ad%e8%a8%80%e7%9a%84%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/06\/c-%e8%af%ad%e8%a8%80%e7%9a%84%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86\/","title":{"rendered":"C \u8bed\u8a00\u7684\u5185\u5b58\u7ba1\u7406"},"content":{"rendered":"\n<p> C \u8bed\u8a00\u7684\u5185\u5b58\u7ba1\u7406\u662f\u7cfb\u7edf\u7ea7\u5f00\u53d1\u7684\u6838\u5fc3\u5185\u5bb9\u4e4b\u4e00\uff0c\u5b83\u76f4\u63a5\u5f71\u54cd\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<strong>\u4e0d\u50cf\u9ad8\u7ea7\u8bed\u8a00\uff0cC \u4e0d\u63d0\u4f9b\u81ea\u52a8\u5783\u573e\u56de\u6536\u673a\u5236<\/strong>\uff0c\u6240\u4ee5\u7a0b\u5e8f\u5458\u5fc5\u987b\u624b\u52a8\u5206\u914d\u548c\u91ca\u653e\u5185\u5b58\u3002\u4e0b\u9762\u5c06\u4ece\u4e13\u4e1a\u89d2\u5ea6\uff0c<strong>\u5168\u9762\u3001\u8be6\u7ec6\u3001\u6743\u5a01<\/strong>\u5730\u89e3\u6790 C \u7684\u5185\u5b58\u7ba1\u7406\u673a\u5236\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcd8 \u4e00\u3001C \u8bed\u8a00\u7684\u5185\u5b58\u5206\u533a\u6a21\u578b<\/h2>\n\n\n\n<p>C \u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u5185\u5b58\u4e00\u822c\u5206\u4e3a\u4ee5\u4e0b\u4e94\u4e2a\u533a\u57df\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u533a\u57df<\/th><th>\u5185\u5bb9<\/th><th>\u751f\u547d\u5468\u671f<\/th><\/tr><\/thead><tbody><tr><td>\ud83e\uddf5 <strong>\u4ee3\u7801\u6bb5\uff08Text Segment\uff09<\/strong><\/td><td>\u7a0b\u5e8f\u7684\u673a\u5668\u6307\u4ee4<\/td><td>\u7a0b\u5e8f\u6267\u884c\u671f\u95f4\u56fa\u5b9a<\/td><\/tr><tr><td>\ud83d\udce6 <strong>\u6570\u636e\u6bb5\uff08Data Segment\uff09<\/strong><\/td><td>\u5168\u5c40\u53d8\u91cf\u3001\u9759\u6001\u53d8\u91cf\uff08\u5df2\u521d\u59cb\u5316\uff09<\/td><td>\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4<\/td><\/tr><tr><td>\ud83e\ude99 <strong>BSS \u6bb5<\/strong><\/td><td>\u5168\u5c40\u53d8\u91cf\u3001\u9759\u6001\u53d8\u91cf\uff08\u672a\u521d\u59cb\u5316\uff09<\/td><td>\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\uff0c\u81ea\u52a8\u7f6e 0<\/td><\/tr><tr><td>\ud83d\udcda <strong>\u5806\uff08Heap\uff09<\/strong><\/td><td>\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\uff08\u5982 <code>malloc<\/code>\uff09<\/td><td>\u624b\u52a8\u7ba1\u7406\uff08\u7a0b\u5e8f\u5458\u8d1f\u8d23\u91ca\u653e\uff09<\/td><\/tr><tr><td>\ud83d\udd01 <strong>\u6808\uff08Stack\uff09<\/strong><\/td><td>\u51fd\u6570\u8c03\u7528\u4e34\u65f6\u53d8\u91cf\u3001\u53c2\u6570<\/td><td>\u51fd\u6570\u8c03\u7528\u81ea\u52a8\u521b\u5efa\uff0c\u8fd4\u56de\u81ea\u52a8\u91ca\u653e<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udd17 \u53c2\u8003\u8d44\u6599\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/C_memory_model\">https:\/\/en.wikipedia.org\/wiki\/C_memory_model<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcda \u4e8c\u3001\u52a8\u6001\u5185\u5b58\u7ba1\u7406\u51fd\u6570\uff08stdlib.h\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>malloc(size_t size)<\/code><\/td><td>\u5206\u914d\u4e00\u5757\u5927\u5c0f\u4e3a <code>size<\/code> \u5b57\u8282\u7684\u5185\u5b58\uff0c\u8fd4\u56de <code>void*<\/code><\/td><\/tr><tr><td><code>calloc(n, size)<\/code><\/td><td>\u5206\u914d n \u4e2a\u5927\u5c0f\u4e3a size \u7684\u7a7a\u95f4\u5e76\u521d\u59cb\u5316\u4e3a 0<\/td><\/tr><tr><td><code>realloc(ptr, new_size)<\/code><\/td><td>\u8c03\u6574\u4e4b\u524d <code>malloc\/calloc<\/code> \u5206\u914d\u7684\u5185\u5b58\u5927\u5c0f<\/td><\/tr><tr><td><code>free(ptr)<\/code><\/td><td>\u91ca\u653e <code>malloc\/calloc\/realloc<\/code> \u5206\u914d\u7684\u5185\u5b58<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u793a\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdlib.h&gt;\n#include &lt;stdio.h&gt;\n\nint main() {\n    int *arr = (int *)malloc(5 * sizeof(int));  \/\/ \u5206\u914d\u7a7a\u95f4\n    if (!arr) {\n        perror(\"Memory allocation failed\");\n        return 1;\n    }\n\n    for (int i = 0; i &lt; 5; i++) {\n        arr&#91;i] = i * 10;\n    }\n\n    arr = (int *)realloc(arr, 10 * sizeof(int));  \/\/ \u91cd\u65b0\u8c03\u6574\u5927\u5c0f\n    if (!arr) {\n        perror(\"Reallocation failed\");\n        return 1;\n    }\n\n    for (int i = 5; i &lt; 10; i++) {\n        arr&#91;i] = i * 10;\n    }\n\n    free(arr);  \/\/ \u5fc5\u987b\u91ca\u653e\n    return 0;\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u26a0\ufe0f \u4e09\u3001\u5185\u5b58\u7ba1\u7406\u5e38\u89c1\u95ee\u9898<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u95ee\u9898<\/th><th>\u89e3\u91ca<\/th><\/tr><\/thead><tbody><tr><td>\u274c \u5185\u5b58\u6cc4\u6f0f<\/td><td>\u5fd8\u8bb0 <code>free()<\/code>\uff0c\u5bfc\u81f4\u5185\u5b58\u65e0\u6cd5\u4f7f\u7528\u4f46\u65e0\u6cd5\u8bbf\u95ee<\/td><\/tr><tr><td>\u274c \u91ce\u6307\u9488\uff08Dangling Pointer\uff09<\/td><td><code>free()<\/code> \u540e\u7ee7\u7eed\u8bbf\u95ee\u539f\u6307\u9488<\/td><\/tr><tr><td>\u274c \u4e8c\u6b21\u91ca\u653e<\/td><td>\u5bf9\u540c\u4e00\u5730\u5740 <code>free()<\/code> \u4e24\u6b21\uff0c\u53ef\u80fd\u5d29\u6e83<\/td><\/tr><tr><td>\u274c \u5185\u5b58\u8d8a\u754c<\/td><td>\u8bbf\u95ee\u5206\u914d\u533a\u57df\u4e4b\u5916\u7684\u5730\u5740<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udd17 \u5de5\u5177\u63a8\u8350\uff1a\u4f7f\u7528 <code>valgrind<\/code> \u68c0\u6d4b\u5185\u5b58\u6cc4\u6f0f<br><a href=\"https:\/\/valgrind.org\/\">https:\/\/valgrind.org\/<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 \u56db\u3001\u9ad8\u7ea7\u6280\u5de7\u4e0e\u5efa\u8bae<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u59cb\u7ec8\u521d\u59cb\u5316\u6307\u9488<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int *p = NULL;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. free \u540e\u7f6e NULL \u9632\u6b62\u60ac\u7a7a\u6307\u9488<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>free(p);\np = NULL;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f7f\u7528\u5185\u5b58\u5206\u914d\u5931\u8d25\u68c0\u67e5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>if (p == NULL) {\n    \/\/ handle allocation failure\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u5efa\u8bae\u4f7f\u7528 <code>calloc<\/code> \u521d\u59cb\u5316\u5185\u5b58\u907f\u514d\u672a\u5b9a\u4e49\u503c<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u4f7f\u7528\u667a\u80fd\u5185\u5b58\u5305\u88c5\u5668\uff08C \u5e76\u4e0d\u539f\u751f\u652f\u6301\uff0c\u4f46\u53ef\u624b\u52a8\u5c01\u88c5\uff09<\/h3>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u4e94\u3001\u5185\u5b58\u7ba1\u7406\u793a\u610f\u56fe\uff08\u6587\u5b57\u7248\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>        \u9ad8\u5730\u5740\n       \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n       \u2502   \u6808(Stack)   \u2502 \u2190 \u81ea\u52a8\u91ca\u653e\uff0c\u5c40\u90e8\u53d8\u91cf\/\u51fd\u6570\u8c03\u7528\u5e27\n       \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n       \u2502              \u2502\n       \u2502    \u7a7a\u95f2\u533a    \u2502 \u2190 \u6808\u5411\u4e0b\u589e\u957f\uff0c\u5806\u5411\u4e0a\u589e\u957f\n       \u2502              \u2502\n       \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n       \u2502    \u5806(Heap)   \u2502 \u2190 malloc\/calloc\/realloc\n       \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n       \u2502 BSS\uff08\u672a\u521d\u59cb\u5316\uff09\u2502\n       \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n       \u2502 \u6570\u636e\u6bb5(\u5df2\u521d\u59cb\u5316)\u2502\n       \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n       \u2502 \u4ee3\u7801\u6bb5\uff08\u51fd\u6570\uff09 \u2502\n       \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n        \u4f4e\u5730\u5740<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd17 \u516d\u3001\u6743\u5a01\u53c2\u8003\u8d44\u6599\u4e0e\u51fa\u7ad9\u94fe\u63a5<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u540d\u79f0<\/th><th>\u94fe\u63a5<\/th><\/tr><\/thead><tbody><tr><td>C \u6807\u51c6\u5e93\uff1astdlib.h \u6587\u6863<\/td><td><a href=\"https:\/\/en.cppreference.com\/w\/c\/memory\">https:\/\/en.cppreference.com\/w\/c\/memory<\/a><\/td><\/tr><tr><td>GNU libc malloc manual<\/td><td><a href=\"https:\/\/www.gnu.org\/software\/libc\/manual\/html_node\/Memory-Allocation.html\">https:\/\/www.gnu.org\/software\/libc\/manual\/html_node\/Memory-Allocation.html<\/a><\/td><\/tr><tr><td>Linux man \u624b\u518c\uff1amalloc<\/td><td><a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/malloc.3.html\">https:\/\/man7.org\/linux\/man-pages\/man3\/malloc.3.html<\/a><\/td><\/tr><tr><td>\u5185\u5b58\u7ba1\u7406\u8be6\u89e3\uff08GeeksForGeeks\uff09<\/td><td><a href=\"https:\/\/www.geeksforgeeks.org\/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc\/\">https:\/\/www.geeksforgeeks.org\/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc\/<\/a><\/td><\/tr><tr><td>Valgrind \u5b98\u7f51<\/td><td><a href=\"https:\/\/valgrind.org\/\">https:\/\/valgrind.org\/<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>C \u8bed\u8a00\u7684\u5185\u5b58\u7ba1\u7406\u662f\u7cfb\u7edf\u7ea7\u5f00\u53d1\u7684\u6838\u5fc3\u5185\u5bb9\u4e4b\u4e00\uff0c\u5b83\u76f4\u63a5\u5f71\u54cd\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002\u4e0d\u50cf\u9ad8\u7ea7\u8bed\u8a00\uff0cC \u4e0d\u63d0\u4f9b\u81ea\u52a8\u5783\u573e\u56de [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3450,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"class_list":["post-3449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3449"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3449\/revisions"}],"predecessor-version":[{"id":3451,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3449\/revisions\/3451"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3450"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}