{"id":51857,"date":"2025-08-11T08:09:33","date_gmt":"2025-08-11T00:09:33","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/51857.html"},"modified":"2025-08-11T08:09:33","modified_gmt":"2025-08-11T00:09:33","slug":"%e8%af%a6%e7%bb%86%e8%af%b4%e6%98%8e-c%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/51857.html","title":{"rendered":"\u8be6\u7ec6\u8bf4\u660e C++\u5185\u5b58\u7ba1\u7406"},"content":{"rendered":"<p>C&#043;&#043;\u5185\u5b58\u7ba1\u7406\u6838\u5fc3\u6458\u8981<\/p>\n<li>1.\n<p>\u200b\u5185\u5b58\u533a\u57df\u200b<\/p>\n<ul>\n<li>\u2022\u5168\u5c40\/\u9759\u6001\u53d8\u91cf \u2192 \u6570\u636e\u6bb5<\/li>\n<li>\u2022\u5c40\u90e8\u53d8\u91cf \u2192 \u6808<\/li>\n<li>\u2022\u52a8\u6001\u5185\u5b58 \u2192 \u5806&#xff08;\u624b\u52a8\u7ba1\u7406&#xff09;<\/li>\n<li>\u2022\u5e38\u91cf \u2192 \u5e38\u91cf\u533a<\/li>\n<\/ul>\n<\/li>\n<li>2.\n<p>\u200bnew\/delete\u64cd\u4f5c<\/p>\n<ul>\n<li>\u2022\u200b\u5185\u7f6e\u7c7b\u578b\u200b&#xff1a;new int(10)\u00a0\u521d\u59cb\u5316&#xff1b;delete[]\u00a0\u91ca\u653e\u6570\u7ec4<\/li>\n<li>\u2022\u200b\u81ea\u5b9a\u4e49\u7c7b\u578b\u200b&#xff1a;\u81ea\u52a8\u8c03\u7528\u6784\u9020\/\u6790\u6784\u51fd\u6570&#xff08;new A\u00a0\u2192 \u6784\u9020&#xff1b;delete\u00a0\u2192 \u6790\u6784&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>3.\n<p>\u200b\u5e95\u5c42\u539f\u7406\u200b<\/p>\n<ul>\n<li>\u2022new\u00a0&#061;\u00a0operator new&#xff08;\u5c01\u88c5malloc&#xff09; &#043; \u6784\u9020\u51fd\u6570<\/li>\n<li>\u2022delete\u00a0&#061; \u6790\u6784\u51fd\u6570 &#043;\u00a0operator delete&#xff08;\u5c01\u88c5free&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>4.\n<p>\u200b**malloc\/free\u00a0vs\u00a0new\/delete**\u200b<\/p>\n<table>\n<tr>\u200b\u533a\u522b\u70b9\u200b\u200b**malloc\/free**\u200b\u200b**new\/delete**\u200b<\/tr>\n<tbody>\n<tr>\n<td>\u200b\u6027\u8d28\u200b<\/td>\n<td>C\u5e93\u51fd\u6570<\/td>\n<td>C&#043;&#043;\u8fd0\u7b97\u7b26<\/td>\n<\/tr>\n<tr>\n<td>\u200b\u521d\u59cb\u5316\u200b<\/td>\n<td>\u274c \u4e0d\u521d\u59cb\u5316<\/td>\n<td>\u2705 \u652f\u6301\u521d\u59cb\u5316<\/td>\n<\/tr>\n<tr>\n<td>\u200b\u6784\u9020\/\u6790\u6784\u200b<\/td>\n<td>\u274c \u4e0d\u8c03\u7528<\/td>\n<td>\u2705 \u81ea\u52a8\u8c03\u7528<\/td>\n<\/tr>\n<tr>\n<td>\u200b\u7c7b\u578b\u5b89\u5168\u200b<\/td>\n<td>\u274c \u9700\u5f3a\u8f6cvoid*<\/td>\n<td>\u2705 \u81ea\u52a8\u63a8\u5bfc\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td>\u200b\u5931\u8d25\u5904\u7406\u200b<\/td>\n<td>\u8fd4\u56deNULL<\/td>\n<td>\u629b\u51fa\u5f02\u5e38<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/li>\n<p>\u200b\u6838\u5fc3\u200b&#xff1a;\u4f18\u5148\u7528new\/delete&#xff08;\u5c24\u5176\u81ea\u5b9a\u4e49\u7c7b\u578b&#xff09;&#xff0c;\u4e25\u683c\u5339\u914d\u4f7f\u7528&#xff08;new[]\u914ddelete[]&#xff09;\u3002<\/p>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"-toc\" style=\"margin-left:0px\">\n<p id=\"%E5%89%8D%E8%A8%80-toc\" style=\"margin-left:0px\">\u524d\u8a00<\/p>\n<p id=\"u0aac24ee-toc\" style=\"margin-left:40px\">\u4e00\u4e2a\u5c0f\u7ec3\u4e60<\/p>\n<p id=\"ua0715f3d-toc\" style=\"margin-left:0px\">1\u3001C&#043;&#043;\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f<\/p>\n<p id=\"ued5107f6-toc\" style=\"margin-left:40px\">1.1 new\/delete\u64cd\u4f5c\u5185\u7f6e\u7c7b\u578b<\/p>\n<p id=\"ue903ff73-toc\" style=\"margin-left:40px\">1.2 new\u548cdelete\u64cd\u4f5c\u81ea\u5b9a\u4e49\u7c7b\u578b<\/p>\n<p id=\"u89bccd2e-toc\" style=\"margin-left:0px\">2\u3001 operator new\u4e0eoperator delete\u51fd\u6570<\/p>\n<p id=\"uc290ce38-toc\" style=\"margin-left:40px\">2.1 operator new\u4e0eoperator delete\u51fd\u6570&#xff08;\u91cd\u70b9&#xff09;<\/p>\n<p id=\"u83c891c3-toc\" style=\"margin-left:0px\">3\u3001 new\u548cdelete\u7684\u5b9e\u73b0\u539f\u7406<\/p>\n<p id=\"uda9910d5-toc\" style=\"margin-left:40px\">3.1 \u5185\u7f6e\u7c7b\u578b<\/p>\n<p id=\"uf86bad77-toc\" style=\"margin-left:40px\">3 .2 \u81ea\u5b9a\u4e49\u7c7b\u578b<\/p>\n<p id=\"u0af9c361-toc\" style=\"margin-left:80px\">3.2.1 new\u7684\u539f\u7406<\/p>\n<p id=\"ud4ef591b-toc\" style=\"margin-left:80px\">3.2.2 delete\u7684\u539f\u7406<\/p>\n<p id=\"u713cd3c1-toc\" style=\"margin-left:80px\">3.2.3 new T[N]\u7684\u539f\u7406<\/p>\n<p id=\"u7474d7c6-toc\" style=\"margin-left:80px\">3.2.4 delete[]\u7684\u539f\u7406<\/p>\n<p id=\"u74a0b478-toc\" style=\"margin-left:0px\">4\u3001 malloc\/free\u548cnew\/delete\u7684\u533a\u522b<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 id=\"%E5%89%8D%E8%A8%80\">\u524d\u8a00<\/h2>\n<h3 id=\"u0aac24ee\">\u4e00\u4e2a\u5c0f\u7ec3\u4e60<\/h3>\n<p>int globalVar &#061; 1;<br \/>\nstatic int staticGlobalVar &#061; 1;<br \/>\nvoid Test()<br \/>\n{<br \/>\nstatic int staticVar &#061; 1;<br \/>\nint localVar &#061; 1;<br \/>\nint num1[10] &#061; { 1, 2, 3, 4 };<br \/>\nchar char2[] &#061; &#034;abcd&#034;;<br \/>\nconst char* pChar3 &#061; &#034;abcd&#034;;<br \/>\nint* ptr1 &#061; (int*)malloc(sizeof(int) * 4);<br \/>\nint* ptr2 &#061; (int*)calloc(4, sizeof(int));<br \/>\nint* ptr3 &#061; (int*)realloc(ptr2, sizeof(int) * 4);<br \/>\nfree(ptr1);<br \/>\nfree(ptr3);<br \/>\n} <\/p>\n<p id=\"u654a5444\">globalVar\u5728\u54ea\u91cc&#xff1f;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u9759\u6001\u533a<\/p>\n<p id=\"u946544b3\">staticGlobalVar\u5728\u54ea\u91cc&#xff1f;\u00a0 \u00a0 \u00a0 \u00a0\u00a0 \u9759\u6001\u533a<\/p>\n<p id=\"u2b27ac4b\">staticVar\u5728\u54ea\u91cc&#xff1f;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u9759\u6001\u533a<\/p>\n<p id=\"udc8f8017\">localVar\u5728\u54ea\u91cc&#xff1f;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u6808<\/p>\n<p id=\"u2bfb4e67\">num1 \u5728\u54ea\u91cc&#xff1f;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u6808<\/p>\n<p id=\"u41f7c152\">char2\u5728\u54ea\u91cc&#xff1f;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u6808<\/p>\n<p id=\"u3d13d161\">*char2\u5728\u54ea\u91cc&#xff1f;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u6808<\/p>\n<p id=\"ub0ec2df0\">pChar3\u5728\u54ea\u91cc&#xff1f;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u6808<\/p>\n<p id=\"u60323e05\">*pChar3\u5728\u54ea\u91cc&#xff1f;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u5e38\u91cf\u533a<\/p>\n<p id=\"u316bd654\">ptr1\u5728\u54ea\u91cc&#xff1f;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u6808<\/p>\n<p id=\"uc0970782\">*ptr1\u5728\u54ea\u91cc&#xff1f;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u5806<\/p>\n<p>tips&#xff1a;\u8fd9\u6837\u5199\u7eaf\u662f\u4e3a\u4e86\u8003\u4f60&#xff0c;\u6ca1\u6709\u610f\u4e49\u6bd4\u5982pChar3\u5b58\u5728\u6808\u91cc\u9762\u51fa\u4e86\u6808\u5c31\u9500\u6bc1&#xff0c;\u4f46pChar3\u6307\u5411\u7684\u6570\u636e\u662f\u5e38\u91cf\u533a\u7684\u4e0d\u4f1a\u88ab\u9500\u6bc1&#xff0c;\u90a3\u4e48\u51fa\u4e86test\u6839\u672c\u4e0d\u80fd\u8bbf\u95ee\u5176\u4e2d\u6570\u636e&#xff0c;pChar3\u672c\u8eab\u5c31\u6ca1\u6709\u5b58\u5728\u610f\u4e49\u4e86<\/p>\n<p id=\"u5a4c4a5e\">\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1212\" id=\"P0Z9I\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811000931-689934bb072d5.png\" width=\"2081\" \/><\/p>\n<h2 id=\"ua0715f3d\">1\u3001C&#043;&#043;\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f<\/h2>\n<p id=\"u2a005ef7\">C\u8bed\u8a00\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\u5728C&#043;&#043;\u4e2d\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528&#xff0c;\u4f46\u6709\u4e9b\u5730\u65b9\u5c31\u65e0\u80fd\u4e3a\u529b&#xff0c;\u800c\u4e14\u4f7f\u7528\u8d77\u6765\u6bd4\u8f83\u9ebb\u70e6&#xff0c;\u56e0 \u6b64C&#043;&#043;\u53c8\u63d0\u51fa\u4e86\u81ea\u5df1\u7684\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f&#xff1a;\u901a\u8fc7new\u548cdelete\u64cd\u4f5c\u7b26\u8fdb\u884c\u52a8\u6001\u5185\u5b58\u7ba1\u7406\u3002<\/p>\n<h3 id=\"ued5107f6\">1.1 new\/delete\u64cd\u4f5c\u5185\u7f6e\u7c7b\u578b<\/h3>\n<p>int main()<br \/>\n{<br \/>\n\/\/ \u52a8\u6001\u7533\u8bf7\u4e00\u4e2aint\u7c7b\u578b\u7684\u7a7a\u95f4<br \/>\nint* ptr4 &#061; new int;<br \/>\n\/\/ \u52a8\u6001\u7533\u8bf7\u4e00\u4e2aint\u7c7b\u578b\u7684\u7a7a\u95f4\u5e76\u521d\u59cb\u5316\u4e3a10<br \/>\nint* ptr5 &#061; new int(10);<\/p>\n<p>\/\/ \u52a8\u6001\u7533\u8bf73\u4e2aint\u7c7b\u578b\u7684\u7a7a\u95f4<br \/>\nint* ptr6 &#061; new int[3];<br \/>\nint* ptr7 &#061; new int[3]{ 1,2,3 };<br \/>\nint* ptr8 &#061; new int[5]{ 1,2,3 };<\/p>\n<p>delete ptr4;<br \/>\ndelete ptr5;<br \/>\ndelete[] ptr6;<\/p>\n<p>return 0;<br \/>\n}<\/p>\n<h3 id=\"ue903ff73\">1.2 new\u548cdelete\u64cd\u4f5c\u81ea\u5b9a\u4e49\u7c7b\u578b<\/h3>\n<p>class A<br \/>\n{<br \/>\npublic:<br \/>\nA(int a &#061; 1)<br \/>\n: _a(a)<br \/>\n{<br \/>\ncout &lt;&lt; &#034;A():&#034; &lt;&lt; this &lt;&lt; endl;<br \/>\n}<br \/>\n~A()<br \/>\n{<br \/>\ncout &lt;&lt; &#034;~A():&#034; &lt;&lt; this &lt;&lt; endl;<br \/>\n}<br \/>\nprivate:<br \/>\nint _a;<br \/>\n};<\/p>\n<p>struct ListNode<br \/>\n{<br \/>\nint _val;<br \/>\nListNode* _next;<\/p>\n<p>ListNode(int val)<br \/>\n:_val(val)<br \/>\n, _next(nullptr)<br \/>\n{}<br \/>\n};<\/p>\n<p>int main()<br \/>\n{<br \/>\n\/\/ \u53ea\u7533\u8bf7\u7a7a\u95f4<br \/>\nA* p1 &#061; (A*)malloc(sizeof(A));<\/p>\n<p>\/\/ \u7533\u8bf7\u7a7a\u95f4&#043;\u6784\u9020\u51fd\u6570<br \/>\nA* p2 &#061; new A(1);<br \/>\nA* p3 &#061; new A;<\/p>\n<p>\/\/ \u53ea\u91ca\u653e\u7a7a\u95f4<br \/>\nfree(p1);<\/p>\n<p>\/\/ \u6790\u6784\u51fd\u6570 &#043; \u91ca\u653e\u7a7a\u95f4<br \/>\ndelete p2;<br \/>\ndelete p3;<\/p>\n<p>\/\/ \u7533\u8bf7\u7a7a\u95f4 &#043; 10\u6b21\u6784\u9020\u51fd\u6570<br \/>\nA* p6 &#061; new A[10];<br \/>\ndelete[] p6;<\/p>\n<p>A aa1(1);<br \/>\nA aa2(2);<br \/>\n\/\/A* p7 &#061; new A[10]{aa1, aa2};<br \/>\nA* p7 &#061; new A[10]{ 1, 2 };\/\/\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362<br \/>\ndelete[] p7;<\/p>\n<p>ListNode* n1 &#061; new ListNode(1);<br \/>\nListNode* n2 &#061; new ListNode(2);<br \/>\nListNode* n3 &#061; new ListNode(3);<br \/>\nListNode* n4 &#061; new ListNode(4);<br \/>\nn1-&gt;_next &#061; n2;<br \/>\nn2-&gt;_next &#061; n3;<br \/>\nn3-&gt;_next &#061; n4;<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<h2 id=\"u89bccd2e\">2\u3001 operator new\u4e0eoperator delete\u51fd\u6570<\/h2>\n<h3 id=\"uc290ce38\">2.1 operator new\u4e0eoperator delete\u51fd\u6570&#xff08;\u91cd\u70b9&#xff09;<\/h3>\n<p id=\"u1bec9e72\">new\u548cdelete\u662f\u7528\u6237\u8fdb\u884c\u52a8\u6001\u5185\u5b58\u7533\u8bf7\u548c\u91ca\u653e\u7684\u64cd\u4f5c\u7b26&#xff0c;operator new \u548coperator delete\u662f \u7cfb\u7edf\u63d0\u4f9b\u7684\u5168\u5c40\u51fd\u6570&#xff0c;new\u5728\u5e95\u5c42\u8c03\u7528operator new\u5168\u5c40\u51fd\u6570\u6765\u7533\u8bf7\u7a7a\u95f4&#xff0c;delete\u5728\u5e95\u5c42\u901a\u8fc7 operator delete\u5168\u5c40\u51fd\u6570\u6765\u91ca\u653e\u7a7a\u95f4\u3002<\/p>\n<p>\/*<br \/>\n operator new&#xff1a;\u8be5\u51fd\u6570\u5b9e\u9645\u901a\u8fc7malloc\u6765\u7533\u8bf7\u7a7a\u95f4&#xff0c;\u5f53malloc\u7533\u8bf7\u7a7a\u95f4\u6210\u529f\u65f6\u76f4\u63a5\u8fd4\u56de&#xff1b;\u7533\u8bf7\u7a7a\u95f4<br \/>\n\u5931\u8d25&#xff0c;\u5c1d\u8bd5\u6267\u884c\u7a7a<br \/>\n\u95f4\u4e0d\u8db3\u5e94\u5bf9\u63aa\u65bd&#xff0c;\u5982\u679c\u6539\u5e94\u5bf9\u63aa\u65bd\u7528\u6237\u8bbe\u7f6e\u4e86&#xff0c;\u5219\u7ee7\u7eed\u7533\u8bf7&#xff0c;\u5426<br \/>\n*\/<br \/>\nvoid* __CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)<br \/>\n{<br \/>\n\/\/ try to allocate size bytes<br \/>\nvoid* p;<br \/>\nwhile ((p &#061; malloc(size)) &#061;&#061; 0)<br \/>\nif (_callnewh(size) &#061;&#061; 0)<br \/>\n{<br \/>\n\/\/ report no memory<br \/>\n\/\/ \u5982\u679c\u7533\u8bf7\u5185\u5b58\u5931\u8d25\u4e86&#xff0c;\u8fd9\u91cc\u4f1a\u629b\u51fabad_alloc \u7c7b\u578b\u5f02\u5e38<br \/>\nstatic const std::bad_alloc nomem;<br \/>\n_RAISE(nomem);<br \/>\n}<br \/>\nreturn (p);<br \/>\n}<\/p>\n<p> \/*<br \/>\n operator delete: \u8be5\u51fd\u6570\u6700\u7ec8\u662f\u901a\u8fc7free\u6765\u91ca\u653e\u7a7a\u95f4\u7684<br \/>\n*\/<br \/>\nvoid operator delete(void* pUserData)<br \/>\n{<br \/>\n_CrtMemBlockHeader* pHead;<br \/>\nRTCCALLBACK(_RTC_Free_hook, (pUserData, 0));<br \/>\nif (pUserData &#061;&#061; NULL)<br \/>\nreturn;<br \/>\n_mlock(_HEAP_LOCK);  \/* block other threads *\/<br \/>\n__TRY<br \/>\n\/* get a pointer to memory block header *\/<br \/>\npHead &#061; pHdr(pUserData);<br \/>\n\/* verify block type *\/<br \/>\n_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead-&gt;nBlockUse));<br \/>\n_free_dbg(pUserData, pHead-&gt;nBlockUse);<br \/>\n__FINALLY<br \/>\n_munlock(_HEAP_LOCK);  \/* release other threads *\/<br \/>\n__END_TRY_FINALLY<br \/>\nreturn;<br \/>\n}<br \/>\n\/*<br \/>\nfree\u7684\u5b9e\u73b0<br \/>\n*\/<br \/>\n#define   free(p)_free_dbg(p, _NORMAL_BLOCK) <\/p>\n<p id=\"u51b20f34\">\u901a\u8fc7\u4e0a\u8ff0\u4e24\u4e2a\u5168\u5c40\u51fd\u6570\u7684\u5b9e\u73b0\u77e5\u9053&#xff0c;operator new \u5b9e\u9645\u4e5f\u662f\u901a\u8fc7malloc\u6765\u7533\u8bf7\u7a7a\u95f4&#xff0c;\u5982\u679c malloc\u7533\u8bf7\u7a7a\u95f4\u6210\u529f\u5c31\u76f4\u63a5\u8fd4\u56de&#xff0c;\u5426\u5219\u6267\u884c\u7528\u6237\u63d0\u4f9b\u7684\u7a7a\u95f4\u4e0d\u8db3\u5e94\u5bf9\u63aa\u65bd&#xff0c;\u5982\u679c\u7528\u6237\u63d0\u4f9b\u8be5\u63aa\u65bd \u5c31\u7ee7\u7eed\u7533\u8bf7&#xff0c;\u5426\u5219\u5c31\u629b\u5f02\u5e38\u3002operator delete \u6700\u7ec8\u662f\u901a\u8fc7free\u6765\u91ca\u653e\u7a7a\u95f4\u7684\u3002<\/p>\n<h2 id=\"u83c891c3\">3\u3001 new\u548cdelete\u7684\u5b9e\u73b0\u539f\u7406<\/h2>\n<h3 id=\"uda9910d5\">3.1 \u5185\u7f6e\u7c7b\u578b<\/h3>\n<p id=\"ufd13f4db\">\u5982\u679c\u7533\u8bf7\u7684\u662f\u5185\u7f6e\u7c7b\u578b\u7684\u7a7a\u95f4&#xff0c;new\u548cmalloc&#xff0c;delete\u548cfree\u57fa\u672c\u7c7b\u4f3c&#xff0c;\u4e0d\u540c\u7684\u5730\u65b9\u662f&#xff1a; new\/delete\u7533\u8bf7\u548c\u91ca\u653e\u7684\u662f\u5355\u4e2a\u5143\u7d20\u7684\u7a7a\u95f4&#xff0c;new[]\u548cdelete[]\u7533\u8bf7\u7684\u662f\u8fde\u7eed\u7a7a\u95f4&#xff0c;\u800c\u4e14new\u5728\u7533 \u8bf7\u7a7a\u95f4\u5931\u8d25\u65f6\u4f1a\u629b\u5f02\u5e38&#xff0c;malloc\u4f1a\u8fd4\u56deNULL\u3002<\/p>\n<h3 id=\"uf86bad77\">3 .2 \u81ea\u5b9a\u4e49\u7c7b\u578b<\/h3>\n<h4 id=\"u0af9c361\">3.2.1 new\u7684\u539f\u7406<\/h4>\n<li id=\"ud9355549\">\u8c03\u7528operator new\u51fd\u6570\u7533\u8bf7\u7a7a\u95f4<\/li>\n<li id=\"u11f14fdf\">\u5728\u7533\u8bf7\u7684\u7a7a\u95f4\u4e0a\u6267\u884c\u6784\u9020\u51fd\u6570&#xff0c;\u5b8c\u6210\u5bf9\u8c61\u7684\u6784\u9020<\/li>\n<h4 id=\"ud4ef591b\">3.2.2 delete\u7684\u539f\u7406<\/h4>\n<li id=\"u596e1dd0\">\u5728\u7a7a\u95f4\u4e0a\u6267\u884c\u6790\u6784\u51fd\u6570&#xff0c;\u5b8c\u6210\u5bf9\u8c61\u4e2d\u8d44\u6e90\u7684\u6e05\u7406\u5de5\u4f5c<\/li>\n<li id=\"u96e17475\">\u8c03\u7528operator delete\u51fd\u6570\u91ca\u653e\u5bf9\u8c61\u7684\u7a7a\u95f4<\/li>\n<h4 id=\"u713cd3c1\">3.2.3 new T[N]\u7684\u539f\u7406<\/h4>\n<li id=\"u325da0eb\">\u8c03\u7528operator new[]\u51fd\u6570&#xff0c;\u5728operator new[]\u4e2d\u5b9e\u9645\u8c03\u7528operator new\u51fd\u6570\u5b8c\u6210N\u4e2a\u5bf9 \u8c61\u7a7a\u95f4\u7684\u7533\u8bf7<\/li>\n<li id=\"uc49ca54d\">\u5728\u7533\u8bf7\u7684\u7a7a\u95f4\u4e0a\u6267\u884cN\u6b21\u6784\u9020\u51fd\u6570<\/li>\n<h4 id=\"u7474d7c6\">3.2.4 delete[]\u7684\u539f\u7406<\/h4>\n<li id=\"u464e10cb\">\u5728\u91ca\u653e\u7684\u5bf9\u8c61\u7a7a\u95f4\u4e0a\u6267\u884cN\u6b21\u6790\u6784\u51fd\u6570&#xff0c;\u5b8c\u6210N\u4e2a\u5bf9\u8c61\u4e2d\u8d44\u6e90\u7684\u6e05\u7406<\/li>\n<li id=\"u3c85c870\">\u8c03\u7528operator delete[]\u91ca\u653e\u7a7a\u95f4&#xff0c;\u5b9e\u9645\u5728operator delete[]\u4e2d\u8c03\u7528operator delete\u6765\u91ca \u653e\u7a7a\u95f4<\/li>\n<h2 id=\"u74a0b478\">4\u3001 malloc\/free\u548cnew\/delete\u7684\u533a\u522b<\/h2>\n<p id=\"u068152d3\">malloc\/free\u548cnew\/delete\u7684\u5171\u540c\u70b9\u662f&#xff1a;\u90fd\u662f\u4ece\u5806\u4e0a\u7533\u8bf7\u7a7a\u95f4&#xff0c;\u5e76\u4e14\u9700\u8981\u7528\u6237\u624b\u52a8\u91ca\u653e\u3002\u4e0d\u540c\u7684\u5730 \u65b9\u662f&#xff1a;<\/p>\n<li id=\"u15ff8699\">malloc\u548cfree\u662f\u51fd\u6570&#xff0c;new\u548cdelete\u662f\u64cd\u4f5c\u7b26<\/li>\n<li id=\"u99e13058\">malloc\u7533\u8bf7\u7684\u7a7a\u95f4\u4e0d\u4f1a\u521d\u59cb\u5316&#xff0c;new\u53ef\u4ee5\u521d\u59cb\u5316<\/li>\n<li id=\"u813eebbf\">malloc\u7533\u8bf7\u7a7a\u95f4\u65f6&#xff0c;\u9700\u8981\u624b\u52a8\u8ba1\u7b97\u7a7a\u95f4\u5927\u5c0f\u5e76\u4f20\u9012&#xff0c;new\u53ea\u9700\u5728\u5176\u540e\u8ddf\u4e0a\u7a7a\u95f4\u7684\u7c7b\u578b\u5373\u53ef&#xff0c; \u5982\u679c\u662f\u591a\u4e2a\u5bf9\u8c61&#xff0c;[]\u4e2d\u6307\u5b9a\u5bf9\u8c61\u4e2a\u6570\u5373\u53ef<\/li>\n<li id=\"u72c75f67\">malloc\u7684\u8fd4\u56de\u503c\u4e3avoid*, \u5728\u4f7f\u7528\u65f6\u5fc5\u987b\u5f3a\u8f6c&#xff0c;new\u4e0d\u9700\u8981&#xff0c;\u56e0\u4e3anew\u540e\u8ddf\u7684\u662f\u7a7a\u95f4\u7684\u7c7b\u578b<\/li>\n<li id=\"u54853100\">malloc\u7533\u8bf7\u7a7a\u95f4\u5931\u8d25\u65f6&#xff0c;\u8fd4\u56de\u7684\u662fNULL&#xff0c;\u56e0\u6b64\u4f7f\u7528\u65f6\u5fc5\u987b\u5224\u7a7a&#xff0c;new\u4e0d\u9700\u8981&#xff0c;\u4f46\u662fnew\u9700 \u8981\u6355\u83b7\u5f02\u5e38<\/li>\n<li id=\"uc4b00dd2\">\u7533\u8bf7\u81ea\u5b9a\u4e49\u7c7b\u578b\u5bf9\u8c61\u65f6&#xff0c;malloc\/free\u53ea\u4f1a\u5f00\u8f9f\u7a7a\u95f4&#xff0c;\u4e0d\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570\u4e0e\u6790\u6784\u51fd\u6570&#xff0c;\u800cnew \u5728\u7533\u8bf7\u7a7a\u95f4\u540e\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570\u5b8c\u6210\u5bf9\u8c61\u7684\u521d\u59cb\u5316&#xff0c;delete\u5728\u91ca\u653e\u7a7a\u95f4\u524d\u4f1a\u8c03\u7528\u6790\u6784\u51fd\u6570\u5b8c\u6210 \u7a7a\u95f4\u4e2d\u8d44\u6e90\u7684\u6e05\u7406\u91ca\u653e<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb224\u6b21\u3002free(ptr1);free(ptr3);globalVar\u5728\u54ea\u91cc\uff1f\u9759\u6001\u533astaticGlobalVar\u5728\u54ea\u91cc\uff1f\u9759\u6001\u533astaticVar\u5728\u54ea\u91cc\uff1f\u9759\u6001\u533alocalVar\u5728\u54ea\u91cc\uff1f\u6808num1 \u5728\u54ea\u91cc\uff1f\u6808char2\u5728\u54ea\u91cc\uff1f\u6808*char2\u5728\u54ea\u91cc\uff1f\u6808pChar3\u5728\u54ea\u91cc\uff1f\u6808*pChar3\u5728\u54ea\u91cc\uff1f\u5e38\u91cf\u533aptr1\u5728\u54ea\u91cc\uff1f\u6808*ptr1\u5728\u54ea\u91cc\uff1f\u5806\u3002<\/p>\n","protected":false},"author":2,"featured_media":51856,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,190],"topic":[],"class_list":["post-51857","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u8be6\u7ec6\u8bf4\u660e C++\u5185\u5b58\u7ba1\u7406 - \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\/51857.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u8be6\u7ec6\u8bf4\u660e C++\u5185\u5b58\u7ba1\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb224\u6b21\u3002free(ptr1);free(ptr3);globalVar\u5728\u54ea\u91cc\uff1f\u9759\u6001\u533astaticGlobalVar\u5728\u54ea\u91cc\uff1f\u9759\u6001\u533astaticVar\u5728\u54ea\u91cc\uff1f\u9759\u6001\u533alocalVar\u5728\u54ea\u91cc\uff1f\u6808num1 \u5728\u54ea\u91cc\uff1f\u6808char2\u5728\u54ea\u91cc\uff1f\u6808*char2\u5728\u54ea\u91cc\uff1f\u6808pChar3\u5728\u54ea\u91cc\uff1f\u6808*pChar3\u5728\u54ea\u91cc\uff1f\u5e38\u91cf\u533aptr1\u5728\u54ea\u91cc\uff1f\u6808*ptr1\u5728\u54ea\u91cc\uff1f\u5806\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/51857.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-11T00:09:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811000931-689934bb072d5.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\/51857.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/51857.html\",\"name\":\"\u8be6\u7ec6\u8bf4\u660e C++\u5185\u5b58\u7ba1\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-11T00:09:33+00:00\",\"dateModified\":\"2025-08-11T00:09:33+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/51857.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/51857.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/51857.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u8be6\u7ec6\u8bf4\u660e C++\u5185\u5b58\u7ba1\u7406\"}]},{\"@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":"\u8be6\u7ec6\u8bf4\u660e C++\u5185\u5b58\u7ba1\u7406 - \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\/51857.html","og_locale":"zh_CN","og_type":"article","og_title":"\u8be6\u7ec6\u8bf4\u660e C++\u5185\u5b58\u7ba1\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb224\u6b21\u3002free(ptr1);free(ptr3);globalVar\u5728\u54ea\u91cc\uff1f\u9759\u6001\u533astaticGlobalVar\u5728\u54ea\u91cc\uff1f\u9759\u6001\u533astaticVar\u5728\u54ea\u91cc\uff1f\u9759\u6001\u533alocalVar\u5728\u54ea\u91cc\uff1f\u6808num1 \u5728\u54ea\u91cc\uff1f\u6808char2\u5728\u54ea\u91cc\uff1f\u6808*char2\u5728\u54ea\u91cc\uff1f\u6808pChar3\u5728\u54ea\u91cc\uff1f\u6808*pChar3\u5728\u54ea\u91cc\uff1f\u5e38\u91cf\u533aptr1\u5728\u54ea\u91cc\uff1f\u6808*ptr1\u5728\u54ea\u91cc\uff1f\u5806\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/51857.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-11T00:09:33+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811000931-689934bb072d5.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\/51857.html","url":"https:\/\/www.wsisp.com\/helps\/51857.html","name":"\u8be6\u7ec6\u8bf4\u660e C++\u5185\u5b58\u7ba1\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-11T00:09:33+00:00","dateModified":"2025-08-11T00:09:33+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/51857.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/51857.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/51857.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u8be6\u7ec6\u8bf4\u660e C++\u5185\u5b58\u7ba1\u7406"}]},{"@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\/51857","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=51857"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/51857\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/51856"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=51857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=51857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=51857"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=51857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}