{"id":345,"date":"2020-11-03T11:09:08","date_gmt":"2020-11-03T15:09:08","guid":{"rendered":"http:\/\/www.prglab.com\/blog\/?page_id=345"},"modified":"2020-11-03T11:09:08","modified_gmt":"2020-11-03T15:09:08","slug":"dynamic-memory","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/cplusplus-tutorial\/compound-data\/dynamic-memory","title":{"rendered":"\u52a8\u6001\u5185\u5b58"},"content":{"rendered":"\n<h2>3.4 \u52a8\u6001\u5185\u5b58\u5206\u914d \uff08Dynamic memory\uff09<\/h2>\n\n\n\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u4e2d\u6211\u4eec\u53ea\u7528\u4e86\u58f0\u660e\u53d8\u91cf\u3001\u6570\u7ec4\u548c\u5176\u4ed6\u5bf9\u8c61\uff08objects\uff09\u6240\u5fc5\u9700\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd9\u4e9b\u5185\u5b58\u7a7a\u95f4\u7684\u5927\u5c0f\u90fd\u5728\u7a0b\u5e8f\u6267\u884c\u4e4b\u524d\u5c31\u5df2\u7ecf\u786e\u5b9a\u4e86\u3002\u4f46\u5982\u679c\u6211\u4eec\u9700\u8981\u5185\u5b58\u5927\u5c0f\u4e3a\u4e00\u4e2a\u53d8\u91cf\uff0c\u5176\u6570\u503c\u53ea\u6709\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6 (runtime)\u624d\u80fd\u786e\u5b9a\uff0c\u4f8b\u5982\u6709\u4e9b\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u6839\u636e\u7528\u6237\u8f93\u5165\u6765\u51b3\u5b9a\u5fc5\u9700\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u90a3\u4e48\u6211\u4eec\u8be5\u600e\u4e48\u529e\u5462\uff1f<\/p>\n\n\n\n<p>\u7b54\u6848\u662f\u52a8\u6001\u5185\u5b58\u5206\u914d\uff08dynamic memory\uff09\uff0c\u4e3a\u6b64C++ \u96c6\u6210\u4e86\u64cd\u4f5c\u7b26new \u548cdelete\u3002<\/p>\n\n\n\n<p><img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/c-diff.png\">&nbsp; \u64cd\u4f5c\u7b26&nbsp;<strong>new<\/strong>&nbsp;\u548c&nbsp;<strong>delete<\/strong>&nbsp;\u662fC++\u6267\u884c\u6307\u4ee4\u3002\u672c\u8282\u540e\u9762\u5c06\u4f1a\u4ecb\u7ecd\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u5728C\u4e2d\u7684\u7b49\u4ef7\u547d\u4ee4\u3002<\/p>\n\n\n\n<h3>\u64cd\u4f5c\u7b26new \u548cnew[ ]<\/h3>\n\n\n\n<p>\u64cd\u4f5c\u7b26new\u7684\u5b58\u5728\u662f\u4e3a\u4e86\u8981\u6c42\u52a8\u6001\u5185\u5b58\u3002new \u540e\u9762\u8ddf\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u8ddf\u4e00\u5bf9\u53ef\u9009\u7684\u65b9\u62ec\u53f7[ ]\u91cc\u9762\u4e3a\u8981\u6c42\u7684\u5143\u7d20\u6570\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5185\u5b58\u5757\u5f00\u59cb\u4f4d\u7f6e\u7684\u6307\u9488\u3002\u5176\u5f62\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<p><code>pointer = new type<\/code><\/p>\n\n\n\n<p>\u6216\u8005<\/p>\n\n\n\n<p><code>pointer = new type [elements]<\/code><\/p>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7528\u6765\u7ed9\u4e00\u4e2a\u5355\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b\u5206\u914d\u5185\u5b58\u3002\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u7528\u6765\u7ed9\u4e00\u4e2a\u6570\u7ec4\u5206\u914d\u5185\u5b58\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1aint * bobby;<br>bobby = new int [5];<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u4e86\u53ef\u5b58\u50a85\u4e2a\u6574\u578bint\u5143\u7d20\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd4\u56de\u6307\u5411\u8fd9\u5757\u7a7a\u95f4\u5f00\u59cb\u4f4d\u7f6e\u7684\u6307\u9488\u5e76\u5c06\u5b83\u8d4b\u7ed9bobby\u3002\u56e0\u6b64\uff0c\u73b0\u5728bobby \u6307\u5411\u4e00\u5757\u53ef\u5b58\u50a85\u4e2a\u6574\u578b\u5143\u7d20\u7684\u5408\u6cd5\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/img3-4-1.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u4f60\u53ef\u80fd\u4f1a\u95ee\u6211\u4eec\u521a\u624d\u6240\u4f5c\u7684\u7ed9\u6307\u9488\u5206\u914d\u5185\u5b58\u7a7a\u95f4\u4e0e\u5b9a\u4e49\u4e00\u4e2a\u666e\u901a\u7684\u6570\u7ec4\u6709\u4ec0\u4e48\u4e0d\u540c\u3002\u6700\u91cd\u8981\u7684\u4e0d\u540c\u662f\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u5fc5\u987b\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u8fd9\u5c31\u5c06\u5b83\u7684\u5927\u5c0f\u5728\u7a0b\u5e8f\u6267\u884c\u4e4b\u524d\u7684\u8bbe\u8ba1\u9636\u6bb5\u5c31\u88ab\u51b3\u5b9a\u4e86\u3002\u800c\u91c7\u7528\u52a8\u6001\u5185\u5b58\u5206\u914d\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u53ef\u4ee5\u5e38\u91cf\u6216\u53d8\u91cf\uff0c\u5176\u503c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u518d\u786e\u5b9a\u3002<\/p>\n\n\n\n<p>\u52a8\u6001\u5185\u5b58\u5206\u914d\u901a\u5e38\u7531\u64cd\u4f5c\u7cfb\u7edf\u63a7\u5236\uff0c\u5728\u591a\u4efb\u52a1\u7684\u73af\u5883\u4e2d\uff0c\u5b83\u53ef\u4ee5\u88ab\u591a\u4e2a\u5e94\u7528\uff08applications\uff09\u5171\u4eab\uff0c\u56e0\u6b64\u5185\u5b58\u6709\u53ef\u80fd\u88ab\u7528\u5149\u3002\u5982\u679c\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c06\u4e0d\u80fd\u5728\u9047\u5230\u64cd\u4f5c\u7b26new \u65f6\u5206\u914d\u6240\u9700\u7684\u5185\u5b58\uff0c\u4e00\u4e2a\u65e0\u6548\u6307\u9488(null pointer)\u5c06\u88ab\u8fd4\u56de\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5efa\u8bae\u5728\u4f7f\u7528new\u4e4b\u540e\u603b\u662f\u68c0\u67e5\u8fd4\u56de\u7684\u6307\u9488\u662f\u5426\u4e3a\u7a7a(null)\uff0c\u5982\u4e0b\u4f8b\u6240\u793a\uff1aint * bobby;<br>bobby = new int [5];<br>if (bobby == NULL) {<br>\/\/ error assigning memory. Take measures.<br>};<\/p>\n\n\n\n<h3>\u5220\u9664\u64cd\u4f5c\u7b26delete<\/h3>\n\n\n\n<p>\u65e2\u7136\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u53ea\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u7684\u67d0\u4e00\u5177\u4f53\u9636\u6bb5\u624d\u6709\u7528\uff0c\u90a3\u4e48\u4e00\u65e6\u5b83\u4e0d\u518d\u88ab\u9700\u8981\u65f6\u5c31\u5e94\u8be5\u88ab\u91ca\u653e\uff0c\u4ee5\u4fbf\u7ed9\u540e\u9762\u7684\u5185\u5b58\u7533\u8bf7\u4f7f\u7528\u3002\u64cd\u4f5c\u7b26delete \u56e0\u6b64\u800c\u4ea7\u751f\uff0c\u5b83\u7684\u5f62\u5f0f\u662f\uff1a<\/p>\n\n\n\n<p><code>delete pointer;<\/code><\/p>\n\n\n\n<p>\u6216<\/p>\n\n\n\n<p><code>delete [ ] pointer;<\/code><\/p>\n\n\n\n<p>\u7b2c\u4e00\u79cd\u8868\u8fbe\u5f62\u5f0f\u7528\u6765\u5220\u9664\u7ed9\u5355\u4e2a\u5143\u7d20\u5206\u914d\u7684\u5185\u5b58\uff0c\u7b2c\u4e8c\u79cd\u8868\u8fbe\u5f62\u5f0f\u7528\u6765\u5220\u9664\u591a\u5143\u7d20\uff08\u6570\u7ec4\uff09\u7684\u5185\u5b58\u5206\u914d\u3002\u5728\u591a\u6570\u7f16\u8bd1\u5668\u4e2d\u4e24\u79cd\u8868\u8fbe\u5f0f\u7b49\u4ef7\uff0c\u4f7f\u7528\u6ca1\u6709\u533a\u522b\uff0c \u867d\u7136\u5b83\u4eec\u5b9e\u9645\u4e0a\u662f\u4e24\u79cd\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u9700\u8981\u8003\u8651\u64cd\u4f5c\u7b26\u91cd\u8f7doverloading \uff08\u6211\u4eec\u5728\u540e\u9762\u7684<a href=\"http:\/\/www.shorelinesolution.com\/cplusplus_tutorial\/tut4-2.jsp\">section 4.2<\/a>\u8282\u4e2d\u5c06\u4f1a\u770b\u5230)\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ rememb-o-matic<br>#include \u2039iostream.h\u203a<br>#include \u2039stdlib.h\u203a<br><br>int main ( ) {<br>char input [100];<br>int i,n;<br>long * l;<br>cout &lt;&lt; &#8220;How many numbers do you want to type in? &#8220;;<br>cin.getline (input,100); i=atoi (input);<br>l= new long[i];<br>if (l == NULL) exit (1);<br>for (n=0; n&lt;i; n++) {<br>cout &lt;&lt; &#8220;Enter number: &#8220;;<br>cin.getline (input,100);<br>l[n]=atol (input);<br>}<br>cout &lt;&lt; &#8220;You have entered: &#8220;;<br>for (n=0; n&lt;i; n++) {<br>cout &lt;&lt; l[n] &lt;&lt; &#8220;, &#8220;;<br>delete[] l;<br>return 0;<br>}<\/td><td>How many numbers do you want to type in? 5<br>Enter number : 75<br>Enter number : 436<br>Enter number : 1067<br>Enter number : 8<br>Enter number : 32<br>You have entered: 75, 436, 1067, 8, 32,<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u53ef\u4ee5\u8bb0\u4e0b\u7528\u6237\u60f3\u8f93\u5165\u7684\u4efb\u610f\u591a\u4e2a\u6570\u5b57\uff0c\u5b83\u7684\u5b9e\u73b0\u5f52\u529f\u4e8e\u6211\u4eec\u52a8\u6001\u5730\u5411\u7cfb\u7edf\u7533\u8bf7\u7528\u6237\u8981\u8f93\u5165\u7684\u6570\u5b57\u6240\u9700\u7684\u7a7a\u95f4\u3002<\/p>\n\n\n\n<p><strong>NULL<\/strong>\u662fC++\u5e93\u4e2d\u5b9a\u4e49\u7684\u4e00\u4e2a\u5e38\u91cf\uff0c\u4e13\u95e8\u8bbe\u8ba1\u7528\u6765\u6307\u4ee3\u7a7a\u6307\u9488\u7684\u3002\u5982\u679c\u8fd9\u4e2a\u5e38\u91cf\u6ca1\u6709\u88ab\u9884\u5148\u5b9a\u4e49\uff0c\u4f60\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4ee5\u5b83\u4e3a0:<\/p>\n\n\n\n<p><code>#define NULL 0<\/code><\/p>\n\n\n\n<p>\u5728\u68c0\u67e5\u6307\u9488\u7684\u65f6\u5019\uff0c0\u548cNULL\u5e76\u6ca1\u6709\u533a\u522b\u3002\u4f46\u7528NULL \u6765\u8868\u793a\u7a7a\u6307\u9488\u66f4\u4e3a\u5e38\u7528\uff0c\u5e76\u4e14\u66f4\u6613\u61c2\u3002\u539f\u56e0\u662f\u6307\u9488\u5f88\u5c11\u88ab\u7528\u6765\u6bd4\u8f83\u5927\u5c0f\u6216\u88ab\u76f4\u63a5\u8d4b\u4e88\u4e00\u4e2a\u96640\u4ee5\u5916\u7684\u6570\u5b57\u5e38\u91cf\uff0c\u4f7f\u7528NULL\uff0c\u8fd9\u4e00\u8d4b\u503c\u884c\u4e3a\u5c31\u88ab\u7b26\u53f7\u5316\u4e86\u3002<\/p>\n\n\n\n<h3>ANSI-C \u4e2d\u7684\u52a8\u6001\u5185\u5b58\u7ba1\u7406Dynamic memory in ANSI-C<\/h3>\n\n\n\n<p>\u64cd\u4f5c\u7b26new \u548cdelete \u4ec5\u5728C++\u4e2d\u6709\u6548\uff0c\u800c\u5728C\u8bed\u8a00\u4e2d\u6ca1\u6709\u3002\u5728C\u8bed\u8a00\u4e2d\uff0c\u4e3a\u4e86\u52a8\u6001\u5206\u914d\u5185\u5b58\uff0c\u6211\u4eec\u5fc5\u987b\u6c42\u52a9\u4e8e\u51fd\u6570\u5e93stdlib.h\u3002\u56e0\u4e3a\u8be5\u51fd\u6570\u5e93\u5728C++\u4e2d\u4ecd\u7136\u6709\u6548\uff0c\u5e76\u4e14\u5728\u4e00\u4e9b\u73b0\u5b58\u7684\u7a0b\u5e8f\u4ecd\u7136\u4f7f\u7528\uff0c\u6240\u4ee5\u6211\u4eec\u4e0b\u9762\u5c06\u5b66\u4e60\u4e00\u4e9b\u5173\u4e8e\u8fd9\u4e2a\u51fd\u6570\u5e93\u4e2d\u7684\u51fd\u6570\u7528\u6cd5\u3002<\/p>\n\n\n\n<h3>\u51fd\u6570malloc<\/h3>\n\n\n\n<p>\u8fd9\u662f\u7ed9\u6307\u9488\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u901a\u7528\u51fd\u6570\u3002\u5b83\u7684\u539f\u578b\u662f\uff1a<\/p>\n\n\n\n<p><code>void * malloc (size_t nbytes);<\/code><\/p>\n\n\n\n<p>\u5176\u4e2dnbytes \u662f\u6211\u4eec\u60f3\u8981\u7ed9\u6307\u9488\u5206\u914d\u7684\u5185\u5b58\u5b57\u8282\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2avoid*\u7c7b\u578b\u7684\u6307\u9488\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u7528\u7c7b\u578b\u8f6c\u6362(type cast)\u6765\u628a\u5b83\u8f6c\u6362\u6210\u76ee\u6807\u6307\u9488\u6240\u9700\u8981\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\uff1achar * ronny;<br>ronny = (char *) malloc (10);<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u5c06\u4e00\u4e2a\u6307\u541110\u4e2a\u5b57\u8282\u53ef\u7528\u7a7a\u95f4\u7684\u6307\u9488\u8d4b\u7ed9ronny\u3002\u5f53\u6211\u4eec\u60f3\u7ed9\u4e00\u7ec4\u9664char \u4ee5\u5916\u7684\u7c7b\u578b\uff08\u4e0d\u662f1\u5b57\u8282\u957f\u5ea6\u7684\uff09\u7684\u6570\u503c\u5206\u914d\u5185\u5b58\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u7528\u5143\u7d20\u6570\u4e58\u4ee5\u6bcf\u4e2a\u5143\u7d20\u7684\u957f\u5ea6\u6765\u786e\u5b9a\u6240\u9700\u5185\u5b58\u7684\u5927\u5c0f\u3002\u5e78\u8fd0\u7684\u662f\u6211\u4eec\u6709\u64cd\u4f5c\u7b26sizeof\uff0c\u5b83\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5177\u4f53\u6570\u636e\u7c7b\u578b\u7684\u957f\u5ea6\u3002int * bobby;<br>bobby = (int *) malloc (5 * sizeof(int));<\/p>\n\n\n\n<p>\u8fd9\u4e00\u5c0f\u6bb5\u4ee3\u7801\u5c06\u4e00\u4e2a\u6307\u5411\u53ef\u5b58\u50a85\u4e2aint\u578b\u6574\u6570\u7684\u5185\u5b58\u5757\u7684\u6307\u9488\u8d4b\u7ed9bobby\uff0c\u5b83\u7684\u5b9e\u9645\u957f\u5ea6\u53ef\u80fd\u662f 2\uff0c4\u6216\u66f4\u591a\u5b57\u8282\u6570\uff0c\u53d6\u51b3\u4e8e\u7a0b\u5e8f\u662f\u5728\u4ec0\u4e48\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u88ab\u7f16\u8bd1\u7684\u3002<\/p>\n\n\n\n<h3>\u51fd\u6570calloc<\/h3>\n\n\n\n<p>calloc \u4e0emalloc \u5728\u64cd\u4f5c\u4e0a\u975e\u5e38\u76f8\u4f3c\uff0c\u4ed6\u4eec\u4e3b\u8981\u7684\u533a\u522b\u662f\u5728\u539f\u578b\u4e0a\uff1a<\/p>\n\n\n\n<p><code>void * calloc (size_t nelements, size_t size);<\/code><\/p>\n\n\n\n<p>\u56e0\u4e3a\u5b83\u63a5\u65362\u4e2a\u53c2\u6570\u800c\u4e0d\u662f1\u4e2a\u3002\u8fd9\u4e24\u4e2a\u53c2\u6570\u76f8\u4e58\u88ab\u7528\u6765\u8ba1\u7b97\u6240\u9700\u5185\u5b58\u5757\u7684\u603b\u957f\u5ea6\u3002\u901a\u5e38\u7b2c\u4e00\u4e2a\u53c2\u6570(nelements)\u662f\u5143\u7d20\u7684\u4e2a\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570 (size) \u88ab\u7528\u6765\u8868\u793a\u6bcf\u4e2a\u5143\u7d20\u7684\u957f\u5ea6\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u7528calloc\u5b9a\u4e49bobby\uff1aint * bobby;<br>bobby = (int *) calloc (5, sizeof(int));<\/p>\n\n\n\n<p>malloc \u548ccalloc\u7684\u53e6\u4e00\u70b9\u4e0d\u540c\u5728\u4e8ecalloc \u4f1a\u5c06\u6240\u6709\u7684\u5143\u7d20\u521d\u59cb\u5316\u4e3a0\u3002<\/p>\n\n\n\n<h3>\u51fd\u6570realloc<\/h3>\n\n\n\n<p>\u5b83\u88ab\u7528\u6765\u6539\u53d8\u5df2\u7ecf\u88ab\u5206\u914d\u7ed9\u4e00\u4e2a\u6307\u9488\u7684\u5185\u5b58\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<p><code>void * realloc (void * pointer, size_t size);<\/code><\/p>\n\n\n\n<p>\u53c2\u6570pointer \u7528\u6765\u4f20\u9012\u4e00\u4e2a\u5df2\u7ecf\u88ab\u5206\u914d\u5185\u5b58\u7684\u6307\u9488\u6216\u4e00\u4e2a\u7a7a\u6307\u9488\uff0c\u800c\u53c2\u6570size \u7528\u6765\u6307\u660e\u65b0\u7684\u5185\u5b58\u957f\u5ea6\u3002\u8fd9\u4e2a\u51fd\u6570\u7ed9\u6307\u9488\u5206\u914dsize \u5b57\u8282\u7684\u5185\u5b58\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u80fd\u9700\u8981\u6539\u53d8\u5185\u5b58\u5757\u7684\u5730\u5740\u4ee5\u4fbf\u80fd\u591f\u5206\u914d\u8db3\u591f\u7684\u5185\u5b58\u6765\u6ee1\u8db3\u65b0\u7684\u957f\u5ea6\u8981\u6c42\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6307\u9488\u5f53\u524d\u6240\u6307\u7684\u5185\u5b58\u4e2d\u7684\u6570\u636e\u5185\u5bb9\u5c06\u4f1a\u88ab\u62f7\u8d1d\u5230\u65b0\u7684\u5730\u5740\u4e2d\uff0c\u4ee5\u4fdd\u8bc1\u73b0\u5b58\u6570\u636e\u4e0d\u4f1a\u4e22\u5931\u3002\u51fd\u6570\u8fd4\u56de\u65b0\u7684\u6307\u9488\u5730\u5740\u3002\u5982\u679c\u65b0\u7684\u5185\u5b58\u5c3a\u5bf8\u4e0d\u80fd\u591f\u88ab\u6ee1\u8db3\uff0c\u51fd\u6570\u5c06\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6307\u9488\uff0c\u4f46\u539f\u6765\u53c2\u6570\u4e2d\u7684\u6307\u9488pointer \u53ca\u5176\u5185\u5bb9\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n\n\n\n<h3>\u51fd\u6570 free<\/h3>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u7528\u6765\u91ca\u653e\u88ab\u524d\u9762malloc, calloc \u6216realloc\u6240\u5206\u914d\u7684\u5185\u5b58\u5757\u3002<\/p>\n\n\n\n<p><code>void free (void * pointer);<\/code><\/p>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u8fd9\u4e2a\u51fd\u6570\u53ea\u80fd\u88ab\u7528\u6765\u91ca\u653e\u7531\u51fd\u6570malloc, calloc \u548crealloc\u6240\u5206\u914d\u7684\u7a7a\u95f4\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u53c2\u8003<a href=\"http:\/\/www.cplusplus.com\/ref\/cstdlib\/\">C++ reference for cstdlib<\/a>\u83b7\u5f97\u66f4\u591a\u5173\u4e8e\u8fd9\u4e9b\u51fd\u6570\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>3.4 \u52a8\u6001\u5185\u5b58\u5206\u914d \uff08Dynamic memory\uff09 \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u4e2d\u6211\u4eec\u53ea\u7528\u4e86\u58f0\u660e\u53d8\u91cf\u3001\u6570\u7ec4\u548c\u5176\u4ed6\u5bf9\u8c61\uff08objects\uff09\u6240\u5fc5\u9700\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd9\u4e9b\u5185\u5b58\u7a7a\u95f4\u7684\u5927\u5c0f\u90fd\u5728\u7a0b\u5e8f\u6267\u884c\u4e4b\u524d\u5c31\u5df2\u7ecf\u786e\u5b9a\u4e86\u3002\u4f46\u5982\u679c\u6211\u4eec\u9700\u8981\u5185\u5b58\u5927\u5c0f\u4e3a\u4e00\u4e2a\u53d8\u91cf\uff0c\u5176\u6570\u503c\u53ea\u6709\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6 (runtime)\u624d\u80fd\u786e\u5b9a\uff0c\u4f8b\u5982\u6709\u4e9b\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u6839\u636e\u7528\u6237\u8f93\u5165\u6765\u51b3\u5b9a\u5fc5\u9700\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u90a3\u4e48\u6211\u4eec\u8be5\u600e\u4e48\u529e\u5462\uff1f \u7b54\u6848\u662f\u52a8\u6001\u5185\u5b58\u5206\u914d\uff08dynamic memory\uff09\uff0c\u4e3a\u6b64C++ \u96c6\u6210\u4e86\u64cd\u4f5c\u7b26new \u548cdelete\u3002 &nbsp; \u64cd\u4f5c\u7b26&nbsp;new&nbsp;\u548c&nbsp;delete&nbsp;\u662fC++\u6267\u884c\u6307\u4ee4\u3002\u672c\u8282\u540e\u9762\u5c06\u4f1a\u4ecb\u7ecd\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u5728C\u4e2d\u7684\u7b49\u4ef7\u547d\u4ee4\u3002 \u64cd\u4f5c\u7b26new \u548cnew[ ] \u64cd\u4f5c\u7b26new\u7684\u5b58\u5728\u662f\u4e3a\u4e86\u8981\u6c42\u52a8\u6001\u5185\u5b58\u3002new \u540e\u9762\u8ddf\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u8ddf\u4e00\u5bf9\u53ef\u9009\u7684\u65b9\u62ec\u53f7[ ]\u91cc\u9762\u4e3a\u8981\u6c42\u7684\u5143\u7d20\u6570\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5185\u5b58\u5757\u5f00\u59cb\u4f4d\u7f6e\u7684\u6307\u9488\u3002\u5176\u5f62\u5f0f\u4e3a\uff1a pointer = new type \u6216\u8005 pointer = new type [elements] \u7b2c\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7528\u6765\u7ed9\u4e00\u4e2a\u5355\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b\u5206\u914d\u5185\u5b58\u3002\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u7528\u6765\u7ed9\u4e00\u4e2a\u6570\u7ec4\u5206\u914d\u5185\u5b58\u3002 \u4f8b\u5982\uff1aint * bobby;bobby = new int [5]; \u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u4e86\u53ef\u5b58\u50a85\u4e2a\u6574\u578bint\u5143\u7d20\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd4\u56de\u6307\u5411\u8fd9\u5757\u7a7a\u95f4\u5f00\u59cb\u4f4d\u7f6e\u7684\u6307\u9488\u5e76\u5c06\u5b83\u8d4b\u7ed9bobby\u3002\u56e0\u6b64\uff0c\u73b0\u5728bobby \u6307\u5411\u4e00\u5757\u53ef\u5b58\u50a85\u4e2a\u6574\u578b\u5143\u7d20\u7684\u5408\u6cd5\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002 \u4f60\u53ef\u80fd\u4f1a\u95ee\u6211\u4eec\u521a\u624d\u6240\u4f5c\u7684\u7ed9\u6307\u9488\u5206\u914d\u5185\u5b58\u7a7a\u95f4\u4e0e\u5b9a\u4e49\u4e00\u4e2a\u666e\u901a\u7684\u6570\u7ec4\u6709\u4ec0\u4e48\u4e0d\u540c\u3002\u6700\u91cd\u8981\u7684\u4e0d\u540c\u662f\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u5fc5\u987b\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u8fd9\u5c31\u5c06\u5b83\u7684\u5927\u5c0f\u5728\u7a0b\u5e8f\u6267\u884c\u4e4b\u524d\u7684\u8bbe\u8ba1\u9636\u6bb5\u5c31\u88ab\u51b3\u5b9a\u4e86\u3002\u800c\u91c7\u7528\u52a8\u6001\u5185\u5b58\u5206\u914d\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u53ef\u4ee5\u5e38\u91cf\u6216\u53d8\u91cf\uff0c\u5176\u503c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u518d\u786e\u5b9a\u3002 \u52a8\u6001\u5185\u5b58\u5206\u914d\u901a\u5e38\u7531\u64cd\u4f5c\u7cfb\u7edf\u63a7\u5236\uff0c\u5728\u591a\u4efb\u52a1\u7684\u73af\u5883\u4e2d\uff0c\u5b83\u53ef\u4ee5\u88ab\u591a\u4e2a\u5e94\u7528\uff08applications\uff09\u5171\u4eab\uff0c\u56e0\u6b64\u5185\u5b58\u6709\u53ef\u80fd\u88ab\u7528\u5149\u3002\u5982\u679c\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c06\u4e0d\u80fd\u5728\u9047\u5230\u64cd\u4f5c\u7b26new \u65f6\u5206\u914d\u6240\u9700\u7684\u5185\u5b58\uff0c\u4e00\u4e2a\u65e0\u6548\u6307\u9488(null pointer)\u5c06\u88ab\u8fd4\u56de\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5efa\u8bae\u5728\u4f7f\u7528new\u4e4b\u540e\u603b\u662f\u68c0\u67e5\u8fd4\u56de\u7684\u6307\u9488\u662f\u5426\u4e3a\u7a7a(null)\uff0c\u5982\u4e0b\u4f8b\u6240\u793a\uff1aint * bobby;bobby = new int [5];if (bobby == NULL) {\/\/ error assigning memory. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":335,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/345"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":1,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/345\/revisions"}],"predecessor-version":[{"id":346,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/345\/revisions\/346"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/335"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}