{"id":357,"date":"2024-06-06T19:42:33","date_gmt":"2024-06-06T11:42:33","guid":{"rendered":"https:\/\/www.vanforever.com.cn\/?p=357"},"modified":"2024-06-10T09:46:39","modified_gmt":"2024-06-10T01:46:39","slug":"%e8%af%a6%e7%bb%86%e4%ba%86%e8%a7%a3malloc","status":"publish","type":"post","link":"https:\/\/www.vanforever.com.cn\/?p=357","title":{"rendered":"\u8be6\u7ec6\u4e86\u89e3malloc()"},"content":{"rendered":"<p><div  class='collapse-block shadow-sm collapse-block-transparent collapsed hide-border-left'><div class='collapse-block-title'><span class='collapse-block-title-inner'>\u67e5\u770b\u7279\u8272\u56fe\u7247<\/span><i class='collapse-icon fa fa-angle-down'><\/i><\/div><div class='collapse-block-body' style='display:none;'><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic.vanforever.com.cn\/Vanforever\/20240421\/wallhaven-werm6q_3840x2400.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large\" data-original=\"https:\/\/pic.vanforever.com.cn\/Vanforever\/20240421\/wallhaven-werm6q_3840x2400.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" width=\"3840\" height=\"2400\" \/><\/div><\/div><\/div><br \/>\n<body><\/p>\n<h1 id='\u8be6\u7ec6\u4e86\u89e3malloc'>\u8be6\u7ec6\u4e86\u89e3malloc()<\/h1>\n<h2 id='\u4ec0\u4e48\u662fmalloc'>\u4ec0\u4e48\u662fmalloc()<\/h2>\n<h3 id='\u4e86\u89e3malloc'>\u4e86\u89e3malloc()<\/h3>\n<p>\u5728\u5934\u6587\u4ef6<code>&lt;cstdlib&gt;<\/code>\u548c<code>&lt;stdlib.h&gt;<\/code>\u4e2d\u7684\u5b9a\u4e49\u662f <code>void* malloc(std::size_t size)<\/code> \u548c <code>void* malloc(size_t size)<\/code><\/p>\n<p><code>malloc()<\/code>\u8d1f\u8d23\u5206\u914d<code>size<\/code>\u5b57\u8282\u7684\u672a\u521d\u59cb\u5316\u5185\u5b58<\/p>\n<p>\u82e5\u5206\u914d\u6210\u529f,\u5219\u8fd4\u56de <strong>\u6307\u5411\u5206\u914d\u7684<\/strong> (\u53ef\u4ee5\u5bf9\u4efb\u4f55\u6807\u91cf<strong>\u5bf9\u9f50<\/strong>) \u7684\u5185\u5b58\u5757\u4e2d \u6700\u4f4e\u5b57\u8282\u7684\u6307\u9488<\/p>\n<p>\u82e5size\u4e3a0\uff0c\u5219\u8fd9\u4e2a\u884c\u4e3a\u662f\u5b9e\u73b0\u5b9a\u4e49\u7684\uff0c\u4e5f\u5c31\u662f\u5176\u884c\u4e3a\u5e76\u4e0d\u662f\u5b8c\u5168\u7531 C \u8bed\u8a00\u6807\u51c6\u5b9a\u4e49\u7684\u3002\u8fd9\u610f\u5473\u7740\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u548c\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u4f1a\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u3002\u4e00\u4e9b\u5b9e\u73b0\u53ef\u80fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6307\u9488\uff08<code>NULL<\/code>\uff09\uff0c\u56e0\u4e3a\u5b83\u4eec\u8ba4\u4e3a\u8bf7\u6c42\u96f6\u5b57\u8282\u5185\u5b58\u662f\u6ca1\u6709\u610f\u4e49\u7684\u3002\u800c\u5176\u4ed6\u5b9e\u73b0\u53ef\u80fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u975e\u7a7a\u6307\u9488\u3002\u8fd9\u4e2a\u975e\u7a7a\u6307\u9488\u901a\u5e38\u6307\u5411\u4e00\u4e2a\u5b9e\u9645\u4e0d\u5305\u542b\u4efb\u4f55\u7528\u6237\u53ef\u7528\u5185\u5b58\u7684\u7a7a\u95f4\uff0c\u8fd9\u4e2a\u7a7a\u95f4\u53ef\u80fd\u975e\u5e38\u5c0f\uff0c\u751a\u81f3\u53ef\u80fd\u65e0\u6cd5\u5b89\u5168\u5730\u88ab\u8bbf\u95ee\u6216\u4fee\u6539\u3002<\/p>\n<p>\u5373\u4f7f <code>malloc<\/code> \u8fd4\u56de\u4e86\u4e00\u4e2a\u975e\u7a7a\u6307\u9488\uff0c\u4f60\u4e5f\u4e0d\u5e94\u8be5\u5bf9\u8fd9\u4e2a\u6307\u9488\u8fdb\u884c\u89e3\u5f15\u7528\u64cd\u4f5c\uff08\u5373\u8bd5\u56fe\u8bbf\u95ee\u6216\u4fee\u6539\u5b83\u6240\u6307\u5411\u7684\u5185\u5b58\uff09\uff0c\u56e0\u4e3a\u8fd9\u79cd\u884c\u4e3a\u53ef\u80fd\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\uff0c\u6bd4\u5982\u7a0b\u5e8f\u5d29\u6e83\u3002\u7136\u800c\uff0c\u5373\u4f7f\u8fd4\u56de\u4e86\u4e00\u4e2a\u975e\u7a7a\u6307\u9488\uff0c\u5f53\u4f60\u4e0d\u518d\u9700\u8981\u8fd9\u6bb5\u5185\u5b58\u65f6\uff0c\u4f60\u5e94\u8be5\u5c06\u8fd9\u4e2a\u6307\u9488\u4f20\u9012\u7ed9 <code>free<\/code> \u51fd\u6570\u3002\u8fd9\u6837\u505a\u662f\u4e3a\u4e86\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\uff0c\u5373\u4f7f\u8fd9\u4e2a\u6307\u9488\u5e76\u6ca1\u6709\u771f\u6b63\u6307\u5411\u4efb\u4f55\u6709\u7528\u7684\u5185\u5b58\u3002\u5982\u679c\u4e0d\u8c03\u7528 <code>free<\/code>\uff0c\u4e00\u4e9b\u7cfb\u7edf\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u8fd9\u90e8\u5206\u5185\u5b58\u4ecd\u7136\u88ab\u7a0b\u5e8f\u4f7f\u7528\uff0c\u4ece\u800c\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\u5bfc\u81f4\u5185\u5b58\u8017\u5c3d\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3 id='\u4ec0\u4e48\u662f\u5bf9\u9f50'>\u4ec0\u4e48\u662f\u5bf9\u9f50?<\/h3>\n<p>\u6bcf\u4e2a\u5b8c\u6574<strong>\u5bf9\u8c61\u7c7b\u578b<\/strong>\u62e5\u6709\u4e00\u4e2a\u79f0\u4f5c<strong>\u5bf9\u9f50\u8981\u6c42<\/strong>\u7684\u5c5e\u6027\uff0c\u5b83\u662f\u4e00\u4e2a <code>size_t<\/code> \u7c7b\u578b\u7684\u6574\u6570\u503c\uff0c\u8868\u793a\u6b64\u7c7b\u578b\u5bf9\u8c61\u53ef\u4ee5\u5206\u914d\u7684\u76f8\u7ee7\u5730\u5740\u4e4b\u95f4\u7684\u5b57\u8282\u6570\u3002\u5408\u6cd5\u7684\u5bf9\u9f50\u503c\u662f\u4e8c\u7684\u975e\u8d1f\u6570\u6b21\u5e42\u3002<\/p>\n<h4 id='\u5bf9\u9f50\u8981\u6c42'>\u5bf9\u9f50\u8981\u6c42<\/h4>\n<p>\u6bcf\u4e2a\u6570\u636e\u7c7b\u578b\u90fd\u6709\u4e00\u4e2a\u5bf9\u9f50\u8981\u6c42\uff08alignment requirement\uff09\uff0c\u8fd9\u662f\u6307\u8be5\u7c7b\u578b\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u5206\u914d\u65f6\u5730\u5740\u5e94\u8be5\u9075\u5b88\u7684\u89c4\u5219\u3002\u5bf9\u9f50\u8981\u6c42\u901a\u5e38\u662f\u4e3a\u4e86\u63d0\u9ad8\u5185\u5b58\u8bbf\u95ee\u7684\u6548\u7387\uff0c\u7279\u522b\u662f\u5728\u786c\u4ef6\u5c42\u9762\u4e0a\u3002\u8bb8\u591a\u5904\u7406\u5668\u5728\u8bbf\u95ee\u672a\u5bf9\u9f50\u7684\u6570\u636e\u65f6\u4f1a\u6709\u6027\u80fd\u635f\u5931\uff0c\u751a\u81f3\u53ef\u80fd\u4e0d\u652f\u6301\u672a\u5bf9\u9f50\u8bbf\u95ee\u3002<\/p>\n<p>\u5bf9\u9f50\u8981\u6c42\u662f\u4e00\u4e2a <code>size_t<\/code> \u7c7b\u578b\u7684\u6574\u6570\u503c\uff0c\u8868\u793a\u6b64\u7c7b\u578b\u5bf9\u8c61\u53ef\u4ee5\u5206\u914d\u7684\u76f8\u7ee7\u5730\u5740\u4e4b\u95f4\u7684\u5b57\u8282\u6570\u3002\u8fd9\u4e2a\u503c\u5fc5\u987b\u662f\u4e8c\u7684\u975e\u8d1f\u6570\u6b21\u5e42\uff0c\u5373 2^0, 2^1, 2^2, 2^3, \u2026 \u7b49\u7b49\u3002\u5e38\u89c1\u7684\u5bf9\u9f50\u503c\u5305\u62ec 1, 2, 4, 8, 16, 32, 64 \u5b57\u8282\u7b49\u3002<\/p>\n<p>\u5047\u8bbe\u4e00\u4e2a\u7c7b\u578b\u7684\u5bf9\u9f50\u8981\u6c42\u662f 4 \u5b57\u8282\uff0c\u90a3\u4e48\u5f53\u4f60\u5728\u5185\u5b58\u4e2d\u5206\u914d\u8fd9\u4e2a\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u5bf9\u8c61\u7684\u5730\u5740\u5e94\u8be5\u662f\u4e00\u4e2a 4 \u7684\u500d\u6570\u3002\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u7684\u5730\u5740\u662f 0x1000\uff0c\u90a3\u4e48\u4e0b\u4e00\u4e2a\u540c\u7c7b\u578b\u5bf9\u8c61\u7684\u5730\u5740\u5e94\u8be5\u662f 0x1004\uff0c\u800c\u4e0d\u662f 0x1001 \u6216 0x1002\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u6240\u4ee5\u58f0\u660e\u6570\u636e\u7ed3\u6784\u65f6\uff0c<strong>\u4e0d\u8981\u628a\u5c0f\u6210\u5458\u53c2\u6742\u58f0\u660e\u5728\u5b57\u8282\u5bf9\u9f50\u7684\u6570\u636e\u4e4b\u95f4<\/strong>\uff0c\u5c0f\u6210\u5458\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u80fd\u7701\u53bb\u4e00\u4e9b\u6d6a\u8d39\u7684\u7a7a\u95f4\u3002<\/p>\n<h4 id='\u5bf9\u8c61\u7c7b\u578b'>\u5bf9\u8c61\u7c7b\u578b<\/h4>\n<p>\u5bf9\u8c61\u7c7b\u578b\uff08object type\uff09\u662f\u6307\u6570\u636e\u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c\u5b83\u8868\u793a\u4e86\u4e00\u4e2a\u5177\u4f53\u7684\u5b9e\u4f53\uff0c\u53ef\u4ee5\u662f\u53d8\u91cf\u3001\u51fd\u6570\u3001\u6570\u7ec4\u3001\u7c7b\u5b9e\u4f8b\u7b49\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u4e0b\u9762\u4ee3\u7801\u6240\u793a\uff0c\u7ed3\u6784\u4f53\u7684\u603b\u5927\u5c0f\u4e3a 5 \u5b57\u8282\uff084 \u5b57\u8282\u7684 <code>int<\/code> \u52a0\u4e0a 1 \u5b57\u8282\u7684 <code>char<\/code>\uff09<\/p>\n<p>\u6ee1\u8db3\u7ed3\u6784\u4f53\u7684\u5bf9\u9f50\u8981\u6c42\uff0c\u7f16\u8bd1\u5668\u4f1a\u5728 <code>c<\/code> \u540e\u9762\u6dfb\u52a0 3 \u5b57\u8282\u7684\u586b\u5145\uff08padding\uff09\uff0c\u4f7f\u5f97\u7ed3\u6784\u4f53\u7684\u603b\u5927\u5c0f\u6210\u4e3a 8 \u5b57\u8282\uff0c\u8fd9\u6837\u7ed3\u6784\u4f53\u7684\u7ed3\u675f\u5730\u5740\u4e5f\u662f\u4e00\u4e2a 4 \u5b57\u8282\u7684\u8fb9\u754c\u3002<\/p>\n<pre><code class='language-cpp' lang='cpp'>#include &lt;stdalign.h&gt;\n#include &lt;stdio.h&gt;\n \n\/\/ struct S \u7684\u5bf9\u8c61\u53ef\u4ee5\u5206\u914d\u4e8e\u4efb\u4f55\u5730\u5740\n\/\/ \u56e0\u4e3a S.a \u548c S.b \u53ef\u4ee5\u5206\u914d\u4e8e\u4efb\u4f55\u5730\u5740\nstruct S\n{\n    char a; \/\/ \u6210\u5458\u5bf9\u8c61\u5927\u5c0f\uff1a1\uff0c\u5bf9\u9f50\uff1a1\n    char b; \/\/ \u6210\u5458\u5bf9\u8c61\u5927\u5c0f\uff1a1\uff0c\u5bf9\u9f50\uff1a1\n}; \/\/ \u7ed3\u6784\u4f53\u5bf9\u8c61\u5927\u5c0f\uff1a2\uff0c\u5bf9\u9f50\uff1a1\n \n\/\/ struct X \u7684\u5bf9\u8c61\u5fc5\u987b\u5206\u914d\u4e8e 4\u5b57\u8282\u8fb9\u754c\n\/\/ \u56e0\u4e3a X.n \u5fc5\u987b\u5206\u914d\u4e8e 4 \u5b57\u8282\u8fb9\u754c\n\/\/ \u56e0\u4e3a int \u7684\u5bf9\u9f50\u8981\u6c42\uff08\u901a\u5e38\uff09\u662f 4\nstruct X\n{\n    int n;  \/\/ \u6210\u5458\u5bf9\u8c61\u5927\u5c0f\uff1a4\uff0c\u5bf9\u9f50\uff1a4\n    char c; \/\/ \u6210\u5458\u5bf9\u8c61\u5927\u5c0f\uff1a1\uff0c\u5bf9\u9f50\uff1a1\n    \/\/ \u5269\u4f59\u7684\u4e09\u4e2a\u5b57\u8282\u8fdb\u884c\u7a7a\u4f4d\u586b\u5145\n}; \/\/ \u7ed3\u6784\u4f53\u5bf9\u8c61\u5927\u5c0f\uff1a8\uff0c\u5bf9\u9f50\uff1a4\n \nint main(void)\n{\n    printf(&quot;sizeof(struct S) = %zu\\n&quot;, sizeof(struct S));\n    printf(&quot;alignof(struct S) = %zu\\n&quot;, alignof(struct S));\n    printf(&quot;sizeof(struct X) = %zu\\n&quot;, sizeof(struct X));\n    printf(&quot;alignof(struct X) = %zu\\n&quot;, alignof(struct X));\n}\n<\/code><\/pre>\n<pre><code class='language-cpp' lang='cpp'>sizeof(struct S) = 2\nalignof(struct S) = 1\nsizeof(struct X) = 8\nalignof(struct X) = 4\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2 id='malloc\u7684\u539f\u7406'>malloc()\u7684\u539f\u7406<\/h2>\n<ul>\n<li>\u5f53\u6240\u9700\u5185\u5b58\u5c0f\u4e8e128kb\u65f6\uff0c\u8c03\u7528<code>brk()<\/code>\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u4ece\u5806\u5206\u914d\u5185\u5b58\uff0c\u5176\u5b9e\u5c31\u662f\u5c06\u5806\u9876\u6307\u9488_edata\u5411\u9ad8\u5730\u5740\u79fb\u52a8\uff0c\u83b7\u5f97\u65b0\u7684\u5185\u5b58\uff0c\u8fd9\u4e00\u6b65\u5176\u5b9e\u662f\u8981\u4e86\u66f4\u5927\u7684\u5185\u5b58\uff0c\u5269\u4e0b\u7684\u5f53\u505a\u5185\u5b58\u6c60\u6765\u7ba1\u7406\uff0c\u4e0b\u6b21<code>malloc()<\/code>\u65f6\uff0c\u4f1a\u5148\u4ece\u5185\u5b58\u6c60\u7684\u7a7a\u95f2\u94fe\u8868\u4e2d\u5bfb\u627e\u5408\u9002\u7684\u503c\u3002\u800c\u4e14\u901a\u8fc7<code>brk()<\/code>\u5206\u914d\u7684\u5185\u5b58\uff0c\u5728<code>free()<\/code>\u7684\u65f6\u5019\u4e0d\u4f1a\u88ab\u56de\u6536\uff0c\u800c\u662f\u653e\u56de\u5185\u5b58\u6c60\u91cc\u3002<\/li>\n<li>\u5f53\u5927\u4e8e128kb\u65f6\uff0c\u8c03\u7528<code>mmap()<\/code>\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\uff0c\u5728\u6587\u4ef6\u6620\u5c04\u533a(\u5806\u548c\u6808\u4e2d\u95f4)\u5206\u914d\u5185\u5b58\u3002\u8fd9\u4e2a\u5206\u914d\u7684\u5185\u5b58\uff0c\u5728free\u7684\u65f6\u5019\u4f1a\u628a\u5185\u5b58\u5f52\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u3002<\/li>\n<\/ul>\n<p>\u800c\u4e14\u5462malloc\u5206\u914d\u7684\u662f\u865a\u62df\u5185\u5b58\uff0c\u53ea\u6709\u5728\u8bbf\u95ee\u8fd9\u5757\u865a\u62df\u5185\u5b58\u7684\u65f6\u5019\uff0c\u624d\u4f1a\u901a\u8fc7<strong>\u7f3a\u9875\u4e2d\u65ad<\/strong>\u6620\u5c04\u5230\u7269\u7406\u5185\u5b58\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3 id='\u4f7f\u7528-brk\u5206\u914d'>\u4f7f\u7528 brk()\u5206\u914d<\/h3>\n<p>\u8fdb\u7a0b\u8c03\u7528A=malloc(30K)\u4ee5\u540e\uff0c\u5185\u5bb9\u7a7a\u95f4\u5982\u56fe\u4e0a\u6240\u793a<\/p>\n<p>malloc\u51fd\u6570\u4f1a\u8c03\u7528brk\u7cfb\u7edf\u8c03\u7528\uff0c\u5c06_edata\u6307\u9488\u5f80\u9ad8\u5730\u5740\u63a830K\uff0c\u5c31\u5b8c\u6210\u865a\u62df\u5185\u5b58\u5206\u914d<\/p>\n<p>_edata+30K\u53ea\u662f\u5b8c\u6210\u865a\u62df\u5730\u5740(<strong>\u6ce8\u610f\u53ea\u662f\u865a\u62df\u5730\u5740<\/strong>)\u7684\u5206\u914d\uff0cA\u8fd9\u5757\u5185\u5b58\u73b0\u5728\u8fd8\u662f\u6ca1\u6709\u7269\u7406\u9875\u4e0e\u4e4b\u5bf9\u5e94\u7684\uff0c\u7b49\u5230\u8fdb\u7a0b\u7b2c\u4e00\u6b21\u8bfb\u5199A\u8fd9\u5757\u5185\u5b58\u7684\u65f6\u5019\uff0c\u53d1\u751f\u7f3a\u9875\u4e2d\u65ad\uff0c\u8fd9\u4e2a\u65f6\u5019\uff0c\u5185\u6838\u624d\u5206\u914dA\u8fd9\u5757\u5185\u5b58\u5bf9\u5e94\u7684\u7269\u7406\u9875\u3002<strong>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u7528malloc\u5206\u914d\u4e86A\u8fd9\u5757\u5185\u5bb9\uff0c\u7136\u540e\u4ece\u6765\u4e0d\u8bbf\u95ee\u5b83\uff0c\u90a3\u4e48\uff0cA\u5bf9\u5e94\u7684\u7269\u7406\u9875\u662f\u4e0d\u4f1a\u88ab\u5206\u914d\u7684\u3002<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/pic.vanforever.com.cn\/C%2B%2B\/20240606\/2024060601.jpg\" referrerpolicy=\"no-referrer\"><\/p>\n<p>&nbsp;<\/p>\n<h3 id='\u4f7f\u7528-mmap-\u5206\u914d'>\u4f7f\u7528 mmap() \u5206\u914d<\/h3>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<strong>malloc\u51fd\u6570\u5206\u914d\u5185\u5b58\uff0c\u5982\u679c\u8bf7\u6c42\u5185\u5b58\u5927\u4e8e128K\uff08\u53ef\u7531M_MMAP_THRESHOLD\u9009\u9879\u8c03\u8282\uff09\uff0c\u90a3\u5c31\u4e0d\u662f\u53bb\u63a8_edata\u6307\u9488\u4e86\uff0c\u800c\u662f\u5229\u7528mmap\u7cfb\u7edf\u8c03\u7528\uff0c\u4ece\u5806\u548c\u6808\u7684\u4e2d\u95f4\u5206\u914d\u4e00\u5757\u865a\u62df\u5185\u5b58<\/strong><\/p>\n<p><strong>\u8fd9\u6837\u5b50\u505a\u4e3b\u8981\u662f\u56e0\u4e3a:<\/strong><\/p>\n<p>\u3000\u3000<strong>brk\u5206\u914d\u7684\u5185\u5b58\u9700\u8981\u7b49\u5230\u9ad8\u5730\u5740\u5185\u5b58\u91ca\u653e\u4ee5\u540e\u624d\u80fd\u91ca\u653e\uff08\u4f8b\u5982\uff0c\u5728B\u91ca\u653e\u4e4b\u524d\uff0cA\u662f\u4e0d\u53ef\u80fd\u91ca\u653e\u7684\uff0c\u56e0\u4e3a\u53ea\u6709\u4e00\u4e2a_edata \u6307\u9488\uff0c\u8fd9\u5c31\u662f\u5185\u5b58\u788e\u7247\u4ea7\u751f\u7684\u539f\u56e0\uff0c\u4ec0\u4e48\u65f6\u5019\u7d27\u7f29\u770b\u4e0b\u9762\uff09\uff0c\u800cmmap\u5206\u914d\u7684\u5185\u5b58\u53ef\u4ee5\u5355\u72ec\u91ca\u653e<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/pic.vanforever.com.cn\/C%2B%2B\/20240606\/2024060602.jpg\" referrerpolicy=\"no-referrer\"><\/p>\n<ul>\n<li>\u8fdb\u7a0b\u8c03\u7528free(C)\u4ee5\u540e\uff0cC\u5bf9\u5e94\u7684\u865a\u62df\u5185\u5b58\u548c\u7269\u7406\u5185\u5b58\u4e00\u8d77\u91ca\u653e<\/li>\n<li>\u8fdb\u7a0b\u8c03\u7528free(B)\u4ee5\u540e\uff0c<strong>B\u5bf9\u5e94\u7684\u865a\u62df\u5185\u5b58\u548c\u7269\u7406\u5185\u5b58\u90fd\u6ca1\u6709\u91ca\u653e<\/strong>\uff0c\u56e0\u4e3a\u53ea\u6709\u4e00\u4e2a_edata\u6307\u9488\uff0c\u5982\u679c\u5f80\u56de\u63a8\uff0c\u90a3\u4e48D\u8fd9\u5757\u5185\u5b58\u600e\u4e48\u529e\u5462\uff1f\u5f53\u7136\uff0cB\u8fd9\u5757\u5185\u5b58\uff0c\u662f<strong>\u53ef\u4ee5\u91cd\u7528\u7684<\/strong>\uff0c\u5982\u679c\u8fd9\u4e2a\u65f6\u5019\u518d\u6765\u4e00\u4e2a40K\u7684\u8bf7\u6c42\uff0c\u90a3\u4e48malloc\u5f88\u53ef\u80fd\u5c31\u628aB\u8fd9\u5757\u5185\u5b58\u8fd4\u56de\u56de\u53bb\u4e86<\/li>\n<li>\u8fdb\u7a0b\u8c03\u7528free(D)\u4ee5\u540e,<strong>B\u548cD\u8fde\u63a5\u8d77\u6765\uff0c\u53d8\u6210\u4e00\u5757140K\u7684\u7a7a\u95f2\u5185\u5b58<\/strong>\u3000\u3000<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/pic.vanforever.com.cn\/C%2B%2B\/20240606\/2024060603.jpg\" referrerpolicy=\"no-referrer\"><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff1a<\/p>\n<p>\u3000\u3000<strong>\u5f53\u6700\u9ad8\u5730\u5740\u7a7a\u95f4\u7684\u7a7a\u95f2\u5185\u5b58\u8d85\u8fc7128K\uff08\u53ef\u7531M_TRIM_THRESHOLD\u9009\u9879\u8c03\u8282\uff09\u65f6\uff0c\u6267\u884c\u5185\u5b58\u7d27\u7f29\u64cd\u4f5c\uff08trim\uff09\u3002<\/strong>\u5728\u4e0a\u4e00\u4e2a\u6b65\u9aa4free\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u6700\u9ad8\u5730\u5740\u7a7a\u95f2\u5185\u5b58\u8d85\u8fc7128K\uff0c\u4e8e\u662f\u5185\u5b58\u7d27\u7f29\uff0c\u53d8\u6210\u56fe9\u6240\u793a<\/p>\n<p><\/body><br \/>\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8be6\u7ec6\u4e86\u89e3malloc() \u4ec0\u4e48\u662fmalloc() \u4e86\u89e3malloc() \u5728\u5934\u6587\u4ef6&lt;cstdlib&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4],"class_list":["post-357","post","type-post","status-publish","format-standard","hentry","category-c","tag-c"],"_links":{"self":[{"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=357"}],"version-history":[{"count":2,"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/357\/revisions"}],"predecessor-version":[{"id":367,"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/357\/revisions\/367"}],"wp:attachment":[{"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}