{"id":49998,"date":"2025-07-30T20:52:30","date_gmt":"2025-07-30T12:52:30","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/49998.html"},"modified":"2025-07-30T20:52:30","modified_gmt":"2025-07-30T12:52:30","slug":"c%e8%af%ad%e8%a8%80-%e5%86%85%e5%ad%98%e7%a9%ba%e9%97%b4%e7%9b%b8%e5%85%b3%e5%87%bd%e6%95%b0%ef%bc%88malloc%e5%87%bd%e6%95%b0%e3%80%81free%e5%87%bd%e6%95%b0%e3%80%81realloc%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/49998.html","title":{"rendered":"C\u8bed\u8a00---\u5185\u5b58\u7a7a\u95f4\u76f8\u5173\u51fd\u6570\uff08malloc\u51fd\u6570\u3001free\u51fd\u6570\u3001realloc\u51fd\u6570\u3001calloc\u51fd\u6570\uff09\u3001\u51fd\u6570\u6307\u9488\u3001\u6307\u9488\u7684\u6307\u9488"},"content":{"rendered":"<h3>\u4e00\u3001\u4e0e\u5185\u5b58\u7a7a\u95f4\u76f8\u5173\u7684\u51fd\u6570<\/h3>\n<h4>\u00a0\u00a0 1\u3001malloc \u51fd\u6570<\/h4>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4e00\u822c\u5f62\u5f0f&#xff1a;malloc&#xff08; n * sizeof(\u6570\u636e\u7c7b\u578b)&#xff09;\u3002\u5176\u4e2d n \u4e3a\u8be5\u7a7a\u95f4\u7684\u5143\u7d20\u4e2a\u6570&#xff1b;n * sizeof(\u6570\u636e\u7c7b\u578b) \u4e3a\u8be5\u7a7a\u95f4\u6240\u5360\u5b57\u8282\u7684\u5927\u5c0f\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 malloc \u51fd\u6570\u7528\u6765\u7533\u8bf7\u7a7a\u95f4&#xff0c;\u5e76\u5c06\u8be5\u7a7a\u95f4\u7684\u9996\u5730\u5740\u4f20\u7ed9\u7528\u6237\u3002\u4f7f\u7528\u8be5\u51fd\u6570\u65f6&#xff0c;\u9700\u8981\u5305\u542b\u5934\u6587\u4ef6&#xff1a;#include&lt;stdlib.h&gt;<\/p>\n<p>eg1. \u5728\u5185\u5b58\u4e2d\u5f00\u8f9f\u4e00\u6bb5\u7a7a\u95f4&#xff0c;\u5b58\u653e\u5b57\u7b26\u578b\u6307\u9488\u6570\u7ec4s\u7684\u5185\u5bb9\u00a0<\/p>\n<p>int main(void)<br \/>\n{<br \/>\nconst char *s &#061; &#034;Hello World!&#034;;<br \/>\nchar *p &#061; malloc(strlen(s) &#043; 1);<br \/>\nstrcpy(p, s);<br \/>\nputs(p);<br \/>\n    free(p);<br \/>\n    p &#061; NULL;\/\/\u9632\u6b62p\u6210\u4e3a\u91ce\u6307\u9488<br \/>\n}<\/p>\n<h4>\u00a0\u00a0\u00a02\u3001free \u51fd\u6570<\/h4>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 free \u51fd\u6570\u7528\u6765\u9500\u6bc1\u7a7a\u95f4\u3002free \u51fd\u6570\u9047\u5230\u7a7a\u6307\u9488\u4e0d\u4f1a\u51fa\u9519&#xff0c;\u5176\u9700\u6ee1\u8db3\u4ee5\u4e0b\u4e09\u70b9\u8981\u6c42&#xff1a;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 (1) \u4e0d\u80fd\u5c06\u4ee5\u9500\u6bc1\u7684\u7a7a\u95f4\u8fde\u7eed\u518d\u6b21\u9500\u6bc1&#xff1b;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 (2) \u7533\u8bf7\u7a7a\u95f4\u7684\u6307\u9488\u4e0d\u80fd\u88ab\u4fee\u6539&#xff0c;\u5426\u5219\u4f1a\u5f15\u8d77\u9500\u6bc1\u9519\u8bef&#xff1b;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(3) \u7a0b\u5e8f\u4ee3\u7801\u4e2d\u7684\u7533\u8bf7\u7a7a\u95f4\u4e0e\u9500\u6bc1\u6210\u5bf9\u51fa\u73b0&#xff0c;\u9075\u5faa &#034;\u6709\u501f\u6709\u8fd8&#xff0c;\u518d\u501f\u4e0d\u96be&#034; \u539f\u5219\u3002<\/p>\n<h4>\u00a0\u00a0\u00a03\u3001realloc \u51fd\u6570<\/h4>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4e00\u822c\u5f62\u5f0f&#xff1a;realloc ( void *p, size_t size)\u3002\u5176\u4e2d p \u4e3a\u4e07\u80fd\u6307\u9488&#xff0c;\u662f\u539f\u7a7a\u95f4\u7684\u9996\u5730\u5740&#xff1b;size \u662f\u65b0\u5f00\u7a7a\u95f4\u7684\u5927\u5c0f&#xff0c;\u8ba1\u7b97\u65b9\u5f0f\u4e3a \u201cn*sizeof(\u6570\u636e\u7c7b\u578b)\u201d &#xff0c;n \u4e3a\u6240\u9700\u5143\u7d20\u4e2a\u6570\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 realloc \u51fd\u6570\u7528\u6765\u91cd\u65b0\u5206\u914d&#xff08;\u6269\u5bb9&#xff09;\u3002\u5c06\u539f\u7a7a\u95f4\u7684\u5185\u5bb9\u590d\u5236\u5230\u65b0\u7a7a\u95f4\u7684\u540c\u65f6&#xff0c;\u5c06\u539f\u7a7a\u95f4\u91ca\u653e\u3002\u4e3a\u9632\u6b62\u91ce\u6307\u9488\u7684\u51fa\u73b0&#xff0c;\u8c03\u7528\u8be5\u51fd\u6570\u7684\u65b9\u5f0f\u4e3a&#xff1a;&#xff08;\u53c2\u6570\u90e8\u5206\u81ea\u5df1\u586b\u5165&#xff09;<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"height:20px;width:600px\">\n<tr>char *p;<\/tr>\n<tbody>\n<tr>p &#061; realloc(\u53c2\u6570)&#xff1b;<\/tr>\n<\/tbody>\n<\/table>\n<p>eg2. \u5728\u00a0eg1 \u7684\u57fa\u7840\u4e0a&#xff0c;\u5bf9\u7533\u8bf7\u7a7a\u95f4\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c&#xff0c;\u5c06\u5b57\u7b26\u578b\u6307\u9488\u6570\u7ec4t\u4e2d\u7684\u5185\u5bb9\u8fde\u63a5\u5230p\u540e\u9762<\/p>\n<p>int main(void)<br \/>\n{<br \/>\nconst char *s &#061; &#034;Hello World!&#034;;<\/p>\n<p>char *p &#061; malloc(strlen(s) &#043; 1);<br \/>\nstrcpy(p, s);<br \/>\n    \/\/\u6269\u5bb9<br \/>\nconst char *t &#061; &#034;China&#034;;<br \/>\np &#061; realloc(p, strlen(p) &#043; strlen(t) &#043; 1);<br \/>\nstrcat(p, t);<br \/>\nputs(p);<br \/>\nfree(p);<br \/>\np &#061; NULL;<\/p>\n<p>return 0;<br \/>\n}<\/p>\n<h4>\u00a0 \u00a04\u3001calloc \u51fd\u6570<\/h4>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4e00\u822c\u5f62\u5f0f&#xff1a;calloc (n, sizeof(\u6570\u636e\u7c7b\u578b) )\u3002\u5176\u4e2d n \u4e3a\u7533\u8bf7\u5143\u7d20\u4e2a\u6570&#xff1b;sizeof(\u6570\u636e\u7c7b\u578b) \u4e3a\u6bcf\u4e2a\u5143\u7d20\u6240\u5360\u5b57\u8282\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 calloc \u51fd\u6570\u4e5f\u7528\u6765\u7533\u8bf7\u7a7a\u95f4&#xff0c;\u53ea\u662f\u4e0e malloc \u51fd\u6570\u7684\u8868\u8fbe\u5f62\u5f0f\u4e0d\u540c\u3002\u4f8b\u5982&#xff1a;\u7533\u8bf7\u5927\u5c0f\u4e3a\u00a0n*sizeof(int) \u5b57\u8282\u7684\u5185\u5b58\u7a7a\u95f4&#xff0c;\u4e24\u79cd\u51fd\u6570\u7684\u8868\u8fbe\u5f0f\u5206\u522b\u5982\u4e0b\u6240\u793a&#xff1a;<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"height:20px;width:600px\">\n<tr>char *p &#061; malloc( n * sizeof(int));<\/tr>\n<tbody>\n<tr>char *p &#061; calloc( n, sizeof(int));<\/tr>\n<\/tbody>\n<\/table>\n<p>eg3. \u5148\u7533\u8bf710\u4e2a\u6574\u578b\u5927\u5c0f\u7684\u7a7a\u95f4\u6765\u5b58\u653e \u201c1,2,3,4,5,6,7,8,9,10\u201d&#xff0c;\u518d\u5bf9\u7a7a\u95f4\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c&#xff0c;\u5b58\u653e1\u523020\u7684\u6574\u6570\u00a0<\/p>\n<p>void printArray(int *a, int len)<br \/>\n{<br \/>\nint i;<br \/>\nfor(i &#061; 0;i &lt; len;&#043;&#043;i)<br \/>\n{<br \/>\nprintf(&#034;%d &#034;, a[i]);<br \/>\n}<br \/>\nputs(&#034;&#034;);<br \/>\n}<\/p>\n<p>int main(void)<br \/>\n{<br \/>\nint n &#061; 10;<br \/>\n\/\/int *p &#061; malloc(n * sizeof(int));<br \/>\nint *p &#061; calloc(n, sizeof(int));<br \/>\n    \/\/\u5b58\u653e1\u523010<br \/>\nint i;<br \/>\nfor(i &#061; 0;i &lt; n;&#043;&#043;i)<br \/>\n{<br \/>\n*(p &#043; i) &#061; i &#043; 1;<br \/>\n}<br \/>\n\/\/\u6269\u5bb9\u5b58\u653e1\u523020<br \/>\nint m &#061; 20;<br \/>\np &#061; realloc(p, m * sizeof(int));<br \/>\nfor(i &#061; 10;i &lt; m;&#043;&#043;i)<br \/>\n{<br \/>\np[i] &#061; i &#043; 1;<br \/>\n}<br \/>\nprintArray(p, m);<br \/>\nfree(p);<\/p>\n<p>return 0;<br \/>\n}<\/p>\n<h3>\u4e8c\u3001\u51fd\u6570\u6307\u9488<\/h3>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4e00\u822c\u5f62\u5f0f&#xff1a;\u8fd4\u56de\u503c\u7c7b\u578b (*\u6307\u9488\u540d)(\u53c2\u6570\u5217\u8868)\u3002\u5176\u4e2d\u6307\u9488\u7c7b\u578b\u4e3a\u53bb\u6389\u6307\u9488\u540d\u7684\u5269\u4f59\u90e8\u5206\u3002\u00a0 \u00a0 \u00a0<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u51fd\u6570\u7684\u6307\u9488\u7528\u6765\u5b58\u50a8\u51fd\u6570\u7684\u5165\u53e3\u5730\u5740&#xff08;\u51fd\u6570\u540d&#xff09;&#xff0c;\u901a\u8fc7\u6307\u9488\u53ef\u4ee5\u95f4\u63a5\u8c03\u7528\u51fd\u6570\u3002\u4e3a\u9632\u6b62\u91ce\u6307\u9488\u7684\u51fa\u73b0&#xff0c;\u9700\u8981\u5bf9\u6307\u9488\u8fdb\u884c\u521d\u59cb\u5316&#xff0c;\u5373 \u201c&#061; NULL\u201d \u3002<\/p>\n<p>eg4. \u5b9a\u4e49add\u51fd\u6570&#xff0c;\u8fd0\u7528\u51fd\u6570\u6307\u9488\u5b9e\u73b0add\u51fd\u6570\u7684\u95f4\u63a5\u8c03\u7528<\/p>\n<p>int add(int a, int b)<br \/>\n{<br \/>\nreturn a &#043; b;<br \/>\n}<br \/>\nint main(void)<br \/>\n{<br \/>\nint (*p)(int , int ) &#061; NULL;\/\/*p\u7684\u5706\u62ec\u53f7\u4e0d\u80fd\u7701\u7565<br \/>\np &#061; add;\/\/\u6307\u9488\u53d8\u91cfp\u6307\u5411\u51fd\u6570\u5165\u53e3\u5730\u5740<br \/>\nprintf(&#034;%d\\\\n&#034;, p(10, 20));\/\/\u8c03\u7528add\u51fd\u6570<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4f7f\u7528\u51fd\u6570\u6307\u9488\u5b9e\u73b0\u56de\u8c03\u51fd\u6570\u529f\u80fd&#xff0c;\u5373&#xff1a;\u5c06\u51fd\u6570\u6307\u9488\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u3002<\/p>\n<p>eg5. \u7f16\u5199\u6392\u5e8f\u51fd\u6570&#xff0c;\u5e76\u5728\u6700\u540e\u7684\u4e3b\u51fd\u6570\u4e2d&#xff0c;\u9009\u62e9\u6392\u5e8f\u65b9\u5f0f&#xff08;pow2&#xff1a;\u4ee5\u5e73\u65b9\u5927\u5c0f\u6392\u5e8f&#xff1b; fn&#xff1a;\u4ee5\u5143\u7d20\u672c\u8eab\u5927\u5c0f\u6392\u5e8f&#xff09;&#xff0c;pow2 \u548c fn \u51fd\u6570\u79f0\u4e3a\u56de\u8c03\u51fd\u6570<\/p>\n<p>int pow2(int n)\/\/\u4ee5\u5143\u7d200\u5e73\u65b9\u5927\u5c0f\u6392\u5e8f<br \/>\n{<br \/>\nreturn n * n;<br \/>\n}<\/p>\n<p>int fn(int n)\/\/\u4ee5\u5143\u7d20\u672c\u8eab\u5927\u5c0f\u6392\u5e8f<br \/>\n{<br \/>\nreturn n;<br \/>\n}<\/p>\n<p>void sort(int *a, int len, int (*pfn)(int))<br \/>\n{<br \/>\nint i, j;<br \/>\nfor(i &#061; 0;i &lt; len &#8211; 1;&#043;&#043;i)<br \/>\n{<br \/>\nfor(j &#061; i &#043; 1;j &lt; len;&#043;&#043;j)<br \/>\n{<br \/>\nif(pfn(a[i]) &gt; pfn(a[j]))<br \/>\n{<br \/>\nint t &#061; a[i];<br \/>\na[i] &#061; a[j];<br \/>\na[j] &#061; t;<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\nint main(void)<br \/>\n{<br \/>\nint a[] &#061; {-1,2,-3,4,-5,6,-7,8,-9,0};<br \/>\nint len &#061; sizeof(a) \/ sizeof(*a);<br \/>\nsort(a, len, pow2);<br \/>\nprintArray(a, len);<br \/>\nreturn 0;<br \/>\n}<\/p>\n<h3>\u4e09\u3001\u6307\u9488\u7684\u6307\u9488<\/h3>\n<h4>\u00a0 \u00a01\u3001\u6982\u5ff5\u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/h4>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6307\u9488\u7684\u6307\u9488\u4e5f\u79f0\u4e3a\u4e8c\u7ea7\u6307\u9488&#xff0c;\u5373\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u3002\u6307\u9488\u6570\u7ec4\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570&#xff0c;\u5f62\u53c2\u662f\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5bf9\u4e8e\u4e0b\u56fe\u4e2d\u7684\u4ee3\u7801&#xff0c;\u7a0b\u5e8f\u4f1a\u5728\u7b2c308\u3001309\u884c\u5d29\u6389&#xff0c;\u663e\u793a\u8d8a\u754c\u8bbf\u95ee&#xff0c;\u53ea\u662f\u503c\u4f20\u9012<\/p>\n<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"224\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730125229-688a158da1094.png\" width=\"221\" \/><\/p>\n<p>\u4fee\u6539\u5730\u65b9&#xff1a;(1) \u7b2c\u4e00\u884c\u7684\u201c *p \u201d \u6539\u4e3a \u201c **p\u00a0\u201d &#xff1b;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 (2) \u7b2c\u4e09\u884c\u7684 \u201c\u00a0p &#061;\u00a0\u201d \u6539\u4e3a \u201d *p &#061;\u00a0\u201c&#xff1b;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0(3) \u7b2c\u4e5d\u884c\u7684 getMem(s) \u6539\u4e3a \u201c getMem(&amp;s) \u201d&#xff1b;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u4fee\u6539\u540e\u7684\u4ee3\u7801\u4e3a&#xff1a;<\/p>\n<p>void getMem(char **p)<br \/>\n{<br \/>\n*p &#061; malloc(100);<br \/>\n}<br \/>\nint main(void)<br \/>\n{<br \/>\nchar *s;<br \/>\ngetMem(&amp;s);<br \/>\nstrcpy(s, &#034;Hello&#034;);<br \/>\nputs(s);<br \/>\nreturn 0;<br \/>\n}<\/p>\n<h4>\u00a0\u00a0\u00a02\u3001\u4f8b\u9898<\/h4>\n<p>eg1. \u8fd0\u7528\u6307\u9488\u7684\u6307\u9488\u9009\u51fa\u5b57\u7b26\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c<\/p>\n<p>char *maxStrings(char **p, int len)<br \/>\n{<br \/>\nchar *max;<br \/>\nmax &#061; p[0];<br \/>\nint i;<br \/>\nfor(i &#061; 1;i &lt; len;&#043;&#043;i)<br \/>\n{<br \/>\nif(strcmp(max, p[i]) &lt; 0)<br \/>\n{<br \/>\nmax &#061; p[i];<br \/>\n}<br \/>\n}<br \/>\nreturn max;<br \/>\n}<\/p>\n<p>eg2.\u8fd0\u7528\u6307\u9488\u7684\u6307\u9488\u8fdb\u884c\u5b57\u7b26\u6570\u7ec4\u7684\u9006\u5e8f<\/p>\n<p>void reverseStrings(char **p, int len)<br \/>\n{<br \/>\nint i;<br \/>\nfor(i &#061; 0;i &lt; len \/ 2;&#043;&#043;i)<br \/>\n{<br \/>\nchar *t;<br \/>\nt &#061; p[i];<br \/>\np[i] &#061; p[len &#8211; i &#8211; 1];<br \/>\np[len &#8211; i &#8211; 1] &#061; t;<br \/>\n}<br \/>\n}<\/p>\n<p>eg3. \u8fd0\u7528\u6307\u9488\u7684\u6307\u9488\u8fdb\u884c\u5b57\u7b26\u6570\u7ec4\u7684\u4ea4\u6362<\/p>\n<p>void swap(char **a, char **b)<br \/>\n{<br \/>\nchar *t;<br \/>\nt &#061; *a;<br \/>\n*a &#061; *b;<br \/>\n*b &#061; t;<br \/>\n}<\/p>\n<p>eg4. \u8fd0\u7528\u6307\u9488\u7684\u6307\u9488\u8fdb\u884c\u5b57\u7b26\u6570\u7ec4\u7684\u6392\u5e8f<\/p>\n<p>void sortStrings(char **p, int len)<br \/>\n{<br \/>\nint i, j;<br \/>\nfor(i &#061; 0;i &lt; len &#8211; 1;&#043;&#043;i)<br \/>\n{<br \/>\nfor(j &#061; i &#043; 1;j &lt; len;&#043;&#043;j)<br \/>\n{<br \/>\nif(strcmp(p[i], p[j]) &gt; 0)<br \/>\n{<br \/>\nswap(p &#043; i, p &#043; j);<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p style=\"text-align:center\">\u3010END\u3011<\/p>\n<\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb288\u6b21\uff0c\u70b9\u8d5e2\u6b21\uff0c\u6536\u85cf2\u6b21\u3002size \u662f\u65b0\u5f00\u7a7a\u95f4\u7684\u5927\u5c0f\uff0c\u8ba1\u7b97\u65b9\u5f0f\u4e3a \u201cn*sizeof(\u6570\u636e\u7c7b\u578b)\u201d \uff0cn \u4e3a\u6240\u9700\u5143\u7d20\u4e2a\u6570\u3002\u5176\u4e2d n \u4e3a\u8be5\u7a7a\u95f4\u7684\u5143\u7d20\u4e2a\u6570\uff1b\u5c06\u539f\u7a7a\u95f4\u7684\u5185\u5bb9\u590d\u5236\u5230\u65b0\u7a7a\u95f4\u7684\u540c\u65f6\uff0c\u5c06\u539f\u7a7a\u95f4\u91ca\u653e\u3002\u4e3a\u9632\u6b62\u91ce\u6307\u9488\u7684\u51fa\u73b0\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u7684\u65b9\u5f0f\u4e3a\uff1a\uff08\u53c2\u6570\u90e8\u5206\u81ea\u5df1\u586b\u5165\uff09\u7f16\u5199\u6392\u5e8f\u51fd\u6570\uff0c\u5e76\u5728\u6700\u540e\u7684\u4e3b\u51fd\u6570\u4e2d\uff0c\u9009\u62e9\u6392\u5e8f\u65b9\u5f0f\uff08pow2\uff1a\u4ee5\u5e73\u65b9\u5927\u5c0f\u6392\u5e8f\uff1b\u5148\u7533\u8bf710\u4e2a\u6574\u578b\u5927\u5c0f\u7684\u7a7a\u95f4\u6765\u5b58\u653e \u201c1,2,3,4,5,6,7,8,9,10\u201d\uff0c\u518d\u5bf9\u7a7a\u95f4\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c\uff0c\u5b58\u653e1\u523020\u7684\u6574\u6570\u3002(3) \u7a0b\u5e8f\u4ee3\u7801\u4e2d\u7684\u7533\u8bf7\u7a7a\u95f4\u4e0e\u9500\u6bc1\u6210\u5bf9\u51fa\u73b0\uff0c\u9075\u5faa &quot;\u6709\u501f\u6709\u8fd8\uff0c\u518d\u501f\u4e0d\u96be&quot; \u539f\u5219\u3002<\/p>\n","protected":false},"author":2,"featured_media":49997,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[174,4864,4866,4867,4865],"topic":[],"class_list":["post-49998","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-4864","tag-4866","tag-4867","tag-4865"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C\u8bed\u8a00-\u5185\u5b58\u7a7a\u95f4\u76f8\u5173\u51fd\u6570\uff08malloc\u51fd\u6570\u3001free\u51fd\u6570\u3001realloc\u51fd\u6570\u3001calloc\u51fd\u6570\uff09\u3001\u51fd\u6570\u6307\u9488\u3001\u6307\u9488\u7684\u6307\u9488 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/49998.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C\u8bed\u8a00-\u5185\u5b58\u7a7a\u95f4\u76f8\u5173\u51fd\u6570\uff08malloc\u51fd\u6570\u3001free\u51fd\u6570\u3001realloc\u51fd\u6570\u3001calloc\u51fd\u6570\uff09\u3001\u51fd\u6570\u6307\u9488\u3001\u6307\u9488\u7684\u6307\u9488 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb288\u6b21\uff0c\u70b9\u8d5e2\u6b21\uff0c\u6536\u85cf2\u6b21\u3002size \u662f\u65b0\u5f00\u7a7a\u95f4\u7684\u5927\u5c0f\uff0c\u8ba1\u7b97\u65b9\u5f0f\u4e3a \u201cn*sizeof(\u6570\u636e\u7c7b\u578b)\u201d \uff0cn \u4e3a\u6240\u9700\u5143\u7d20\u4e2a\u6570\u3002\u5176\u4e2d n \u4e3a\u8be5\u7a7a\u95f4\u7684\u5143\u7d20\u4e2a\u6570\uff1b\u5c06\u539f\u7a7a\u95f4\u7684\u5185\u5bb9\u590d\u5236\u5230\u65b0\u7a7a\u95f4\u7684\u540c\u65f6\uff0c\u5c06\u539f\u7a7a\u95f4\u91ca\u653e\u3002\u4e3a\u9632\u6b62\u91ce\u6307\u9488\u7684\u51fa\u73b0\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u7684\u65b9\u5f0f\u4e3a\uff1a\uff08\u53c2\u6570\u90e8\u5206\u81ea\u5df1\u586b\u5165\uff09\u7f16\u5199\u6392\u5e8f\u51fd\u6570\uff0c\u5e76\u5728\u6700\u540e\u7684\u4e3b\u51fd\u6570\u4e2d\uff0c\u9009\u62e9\u6392\u5e8f\u65b9\u5f0f\uff08pow2\uff1a\u4ee5\u5e73\u65b9\u5927\u5c0f\u6392\u5e8f\uff1b\u5148\u7533\u8bf710\u4e2a\u6574\u578b\u5927\u5c0f\u7684\u7a7a\u95f4\u6765\u5b58\u653e \u201c1,2,3,4,5,6,7,8,9,10\u201d\uff0c\u518d\u5bf9\u7a7a\u95f4\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c\uff0c\u5b58\u653e1\u523020\u7684\u6574\u6570\u3002(3) \u7a0b\u5e8f\u4ee3\u7801\u4e2d\u7684\u7533\u8bf7\u7a7a\u95f4\u4e0e\u9500\u6bc1\u6210\u5bf9\u51fa\u73b0\uff0c\u9075\u5faa &quot;\u6709\u501f\u6709\u8fd8\uff0c\u518d\u501f\u4e0d\u96be&quot; \u539f\u5219\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/49998.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-30T12:52:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730125229-688a158da1094.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49998.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/49998.html\",\"name\":\"C\u8bed\u8a00-\u5185\u5b58\u7a7a\u95f4\u76f8\u5173\u51fd\u6570\uff08malloc\u51fd\u6570\u3001free\u51fd\u6570\u3001realloc\u51fd\u6570\u3001calloc\u51fd\u6570\uff09\u3001\u51fd\u6570\u6307\u9488\u3001\u6307\u9488\u7684\u6307\u9488 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-30T12:52:30+00:00\",\"dateModified\":\"2025-07-30T12:52:30+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/49998.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/49998.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49998.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C\u8bed\u8a00---\u5185\u5b58\u7a7a\u95f4\u76f8\u5173\u51fd\u6570\uff08malloc\u51fd\u6570\u3001free\u51fd\u6570\u3001realloc\u51fd\u6570\u3001calloc\u51fd\u6570\uff09\u3001\u51fd\u6570\u6307\u9488\u3001\u6307\u9488\u7684\u6307\u9488\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C\u8bed\u8a00-\u5185\u5b58\u7a7a\u95f4\u76f8\u5173\u51fd\u6570\uff08malloc\u51fd\u6570\u3001free\u51fd\u6570\u3001realloc\u51fd\u6570\u3001calloc\u51fd\u6570\uff09\u3001\u51fd\u6570\u6307\u9488\u3001\u6307\u9488\u7684\u6307\u9488 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/49998.html","og_locale":"zh_CN","og_type":"article","og_title":"C\u8bed\u8a00-\u5185\u5b58\u7a7a\u95f4\u76f8\u5173\u51fd\u6570\uff08malloc\u51fd\u6570\u3001free\u51fd\u6570\u3001realloc\u51fd\u6570\u3001calloc\u51fd\u6570\uff09\u3001\u51fd\u6570\u6307\u9488\u3001\u6307\u9488\u7684\u6307\u9488 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb288\u6b21\uff0c\u70b9\u8d5e2\u6b21\uff0c\u6536\u85cf2\u6b21\u3002size \u662f\u65b0\u5f00\u7a7a\u95f4\u7684\u5927\u5c0f\uff0c\u8ba1\u7b97\u65b9\u5f0f\u4e3a \u201cn*sizeof(\u6570\u636e\u7c7b\u578b)\u201d \uff0cn \u4e3a\u6240\u9700\u5143\u7d20\u4e2a\u6570\u3002\u5176\u4e2d n \u4e3a\u8be5\u7a7a\u95f4\u7684\u5143\u7d20\u4e2a\u6570\uff1b\u5c06\u539f\u7a7a\u95f4\u7684\u5185\u5bb9\u590d\u5236\u5230\u65b0\u7a7a\u95f4\u7684\u540c\u65f6\uff0c\u5c06\u539f\u7a7a\u95f4\u91ca\u653e\u3002\u4e3a\u9632\u6b62\u91ce\u6307\u9488\u7684\u51fa\u73b0\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u7684\u65b9\u5f0f\u4e3a\uff1a\uff08\u53c2\u6570\u90e8\u5206\u81ea\u5df1\u586b\u5165\uff09\u7f16\u5199\u6392\u5e8f\u51fd\u6570\uff0c\u5e76\u5728\u6700\u540e\u7684\u4e3b\u51fd\u6570\u4e2d\uff0c\u9009\u62e9\u6392\u5e8f\u65b9\u5f0f\uff08pow2\uff1a\u4ee5\u5e73\u65b9\u5927\u5c0f\u6392\u5e8f\uff1b\u5148\u7533\u8bf710\u4e2a\u6574\u578b\u5927\u5c0f\u7684\u7a7a\u95f4\u6765\u5b58\u653e \u201c1,2,3,4,5,6,7,8,9,10\u201d\uff0c\u518d\u5bf9\u7a7a\u95f4\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c\uff0c\u5b58\u653e1\u523020\u7684\u6574\u6570\u3002(3) \u7a0b\u5e8f\u4ee3\u7801\u4e2d\u7684\u7533\u8bf7\u7a7a\u95f4\u4e0e\u9500\u6bc1\u6210\u5bf9\u51fa\u73b0\uff0c\u9075\u5faa &quot;\u6709\u501f\u6709\u8fd8\uff0c\u518d\u501f\u4e0d\u96be&quot; \u539f\u5219\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/49998.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-30T12:52:30+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730125229-688a158da1094.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/49998.html","url":"https:\/\/www.wsisp.com\/helps\/49998.html","name":"C\u8bed\u8a00-\u5185\u5b58\u7a7a\u95f4\u76f8\u5173\u51fd\u6570\uff08malloc\u51fd\u6570\u3001free\u51fd\u6570\u3001realloc\u51fd\u6570\u3001calloc\u51fd\u6570\uff09\u3001\u51fd\u6570\u6307\u9488\u3001\u6307\u9488\u7684\u6307\u9488 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-30T12:52:30+00:00","dateModified":"2025-07-30T12:52:30+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/49998.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/49998.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/49998.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C\u8bed\u8a00---\u5185\u5b58\u7a7a\u95f4\u76f8\u5173\u51fd\u6570\uff08malloc\u51fd\u6570\u3001free\u51fd\u6570\u3001realloc\u51fd\u6570\u3001calloc\u51fd\u6570\uff09\u3001\u51fd\u6570\u6307\u9488\u3001\u6307\u9488\u7684\u6307\u9488"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=49998"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49998\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/49997"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=49998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=49998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=49998"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=49998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}