{"id":98887,"date":"2026-09-01T16:01:46","date_gmt":"2026-09-01T08:01:46","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/98887.html"},"modified":"2026-09-01T16:01:46","modified_gmt":"2026-09-01T08:01:46","slug":"c%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/98887.html","title":{"rendered":"c++\u5185\u5b58\u7ba1\u7406"},"content":{"rendered":"<h2>\u5185\u5b58\u7ba1\u7406<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260901080145-6a96866941c85.png\" alt=\"\u5185\u5b58\u533a\u57df\u5212\u5206\" \/><\/p>\n<ul>\n<li>\u6808\u53c8\u53eb\u5806\u6808\u2013\u975e\u9759\u6001\u5c40\u90e8\u53d8\u91cf\/\u51fd\u6570\u53c2\u6570\/\u8fd4\u56de\u503c\u7b49\u7b49&#xff0c;\u6808\u662f\u5411\u4e0b\u589e\u957f\u7684<\/li>\n<li>\u5185\u5b58\u6620\u5c04\u6bb5\u662f\u9ad8\u6548\u7684I\/O\u6620\u5c04\u65b9\u5f0f&#xff0c;\u7528\u4e8e\u88c5\u8f7d\u4e00\u4e2a\u5171\u4eab\u7684\u52a8\u6001\u5185\u5b58\u5e93\u3002\u7528\u6237\u53ef\u4f7f\u7528\u7cfb\u7edf\u63a5\u53e3\u521b\u5efa\u5171\u4eab\u5185\u5b58&#xff0c;\u505a\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/li>\n<li>\u5806\u7528\u4e8e\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a8\u6001\u5185\u5b58\u5206\u914d&#xff0c;\u5806\u5411\u4e0a\u589e\u957f<\/li>\n<li>\u6570\u636e\u6bb5\u2013\u5b58\u50a8\u5168\u5c40\u6570\u636e\u548c\u9759\u6001\u6570\u636e<\/li>\n<li>\u4ee3\u7801\u6bb5\u2013\u53ef\u6267\u884c\u7684\u4ee3\u7801\/\u53ea\u8bfb\u5e38\u91cf<\/li>\n<\/ul>\n<h3>c&#043;&#043;\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f<\/h3>\n<p>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<\/p>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">test<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">int<\/span><span class=\"token operator\">*<\/span> ptr1 <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">int<\/span><span class=\"token operator\">*<\/span> ptr2 <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">int<\/span><span class=\"token operator\">*<\/span> ptr3 <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">[<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">delete<\/span> ptr1<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">delete<\/span> ptr2<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">delete<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> ptr3<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260901080145-6a968669568b5.png\" alt=\"new\/delete\" \/><\/p>\n<ul>\n<li>new[]\u548cdelete[]\u642d\u914d\u4f7f\u7528<\/li>\n<\/ul>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">A<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n<span class=\"token function\">A<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> a <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token operator\">:<\/span><span class=\"token function\">_a<\/span><span class=\"token punctuation\">(<\/span>a<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\ncout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;A():&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token keyword\">this<\/span> <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token operator\">~<\/span><span class=\"token function\">A<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\ncout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;~A():&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token keyword\">this<\/span> <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">GetA<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">return<\/span> _a<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n<span class=\"token keyword\">int<\/span> _a<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\nA<span class=\"token operator\">*<\/span> p1 <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>A<span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token function\">malloc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>A<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">free<\/span><span class=\"token punctuation\">(<\/span>p1<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\nA<span class=\"token operator\">*<\/span> p2 <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token function\">A<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">int<\/span> result <span class=\"token operator\">&#061;<\/span> p2<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">GetA<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\ncout <span class=\"token operator\">&lt;&lt;<\/span> result <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">delete<\/span> p2<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<ul>\n<li>\u5185\u7f6e\u7c7b\u578b\u662f\u51e0\u4e4e\u4e00\u6837\u7684<\/li>\n<li>new\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570&#xff0c;delete\u4f1a\u8c03\u7528\u6790\u6784\u51fd\u6570&#xff0c;malloc\/free\u4e0d\u4f1a<\/li>\n<\/ul>\n<h3>operator new \u4e0e operator delete\u51fd\u6570<\/h3>\n<p>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\u8fc7operator delete\u5168\u5c40\u51fd\u6570\u6765\u91ca\u653e\u7a7a\u95f4\u3002<\/p>\n<p><span class=\"token comment\">\/*<br \/>\noperator 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\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*\/<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span>__CRTDECL <span class=\"token keyword\">operator<\/span> <span class=\"token keyword\">new<\/span><span class=\"token punctuation\">(<\/span>size_t size<span class=\"token punctuation\">)<\/span> <span class=\"token function\">_THROW1<\/span><span class=\"token punctuation\">(<\/span>_STD bad_alloc<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ try to allocate size bytes<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span>p<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span>p <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">malloc<\/span><span class=\"token punctuation\">(<\/span>size<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">_callnewh<\/span><span class=\"token punctuation\">(<\/span>size<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n  <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ report no memory<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5982\u679c\u7533\u8bf7\u5185\u5b58\u5931\u8d25\u4e86&#xff0c;\u8fd9\u91cc\u4f1a\u629b\u51fabad_alloc \u7c7b\u578b\u5f02\u5e38<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">const<\/span> std<span class=\"token double-colon punctuation\">::<\/span>bad_alloc nomem<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">_RAISE<\/span><span class=\"token punctuation\">(<\/span>nomem<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>p<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/*<br \/>\noperator delete: \u8be5\u51fd\u6570\u6700\u7ec8\u662f\u901a\u8fc7free\u6765\u91ca\u653e\u7a7a\u95f4\u7684<br \/>\n*\/<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token keyword\">operator<\/span> <span class=\"token keyword\">delete<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span>pUserData<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n_CrtMemBlockHeader <span class=\"token operator\">*<\/span> pHead<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">RTCCALLBACK<\/span><span class=\"token punctuation\">(<\/span>_RTC_Free_hook<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span>pUserData<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>pUserData <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">_mlock<\/span><span class=\"token punctuation\">(<\/span>_HEAP_LOCK<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/* block other threads *\/<\/span><br \/>\n__TRY<br \/>\n<span class=\"token comment\">\/* get a pointer to memory block header *\/<\/span><br \/>\npHead <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">pHdr<\/span><span class=\"token punctuation\">(<\/span>pUserData<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/* verify block type *\/<\/span><br \/>\n<span class=\"token function\">_ASSERTE<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">_BLOCK_TYPE_IS_VALID<\/span><span class=\"token punctuation\">(<\/span>pHead<span class=\"token operator\">-&gt;<\/span>nBlockUse<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">_free_dbg<\/span><span class=\"token punctuation\">(<\/span> pUserData<span class=\"token punctuation\">,<\/span> pHead<span class=\"token operator\">-&gt;<\/span>nBlockUse <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n__FINALLY<br \/>\n<span class=\"token function\">_munlock<\/span><span class=\"token punctuation\">(<\/span>_HEAP_LOCK<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/* release other threads *\/<\/span><br \/>\n__END_TRY_FINALLY<br \/>\n<span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/*<br \/>\nfree\u7684\u5b9e\u73b0<br \/>\n*\/<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">define<\/span>   <span class=\"token macro-name function\">free<\/span><span class=\"token expression\"><span class=\"token punctuation\">(<\/span>p<span class=\"token punctuation\">)<\/span>               <span class=\"token function\">_free_dbg<\/span><span class=\"token punctuation\">(<\/span>p<span class=\"token punctuation\">,<\/span> _NORMAL_BLOCK<span class=\"token punctuation\">)<\/span><\/span><\/span><\/p>\n<ul>\n<li>operator new\u662f\u5bf9malloc\u7684\u5c01\u88c5,\u5982\u679cmalloc\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<\/li>\n<li>operator delete\u662f\u5bf9free\u7684\u5c01\u88c5<\/li>\n<\/ul>\n<h3>new\u548cdelete\u7684\u5b9e\u73b0\u539f\u7406<\/h3>\n<li>\u5185\u7f6e\u7c7b\u578b \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<\/li>\n<li>\u81ea\u5b9a\u4e49\u7c7b\u578b<\/li>\n<ul>\n<li>new\u7684\u539f\u7406\n<li>\u8c03\u7528operator new\u51fd\u6570\u7533\u8bf7\u7a7a\u95f4<\/li>\n<li>\u5728\u7533\u8bf7\u7684\u7a7a\u95f4\u4e0a\u6267\u884c\u6784\u9020\u51fd\u6570,\u5b8c\u6210\u5bf9\u8c61\u7684\u6784\u9020<\/li>\n<\/li>\n<li>delete\u7684\u539f\u7406\n<li>\u5728\u7a7a\u95f4\u4e0a\u6267\u884c\u6790\u6784\u51fd\u6570,\u5b8c\u6210\u5bf9\u8c61\u4e2d\u8d44\u6e90\u7684\u6e05\u7406\u5de5\u4f5c<\/li>\n<li>\u8c03\u7528operator delete\u51fd\u6570\u91ca\u653e\u5bf9\u8c61\u7684\u7a7a\u95f4<\/li>\n<\/li>\n<li>new T[N] \u548c delete[]\u5c31\u662f\u4e0a\u8ff0\u591a\u4e86N\u6b21\u8c03\u7528<\/li>\n<\/ul>\n<h3>\u5b9a\u4f4dnew\u8868\u8fbe\u5f0f(placement-new)<\/h3>\n<p>\u5b9a\u4f4dnew\u8868\u8fbe\u5f0f\u662f\u5728\u5df2\u5206\u914d\u7684\u539f\u59cb\u5185\u5b58\u7a7a\u95f4\u4e2d\u8c03\u7528\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u4e00\u4e2a\u5bf9\u8c61<\/p>\n<ul>\n<li>\u6790\u6784\u51fd\u6570\u53ef\u4ee5\u4e3b\u52a8\u8c03\u7528,\u6784\u9020\u51fd\u6570\u4e0d\u80fd \u4f7f\u7528\u683c\u5f0f&#xff1a; new (place_address) type\u6216\u8005new (place_address) type(initializer-list) place_address\u5fc5\u987b\u662f\u4e00\u4e2a\u6307\u9488&#xff0c;initializer-list\u662f\u7c7b\u578b\u7684\u521d\u59cb\u5316\u5217\u8868 \u4f7f\u7528\u573a\u666f&#xff1a; \u5b9a\u4f4dnew\u8868\u8fbe\u5f0f\u5728\u5b9e\u9645\u4e2d\u4e00\u822c\u662f\u914d\u5408\u5185\u5b58\u6c60\u4f7f\u7528\u3002\u56e0\u4e3a\u5185\u5b58\u6c60\u5206\u914d\u51fa\u7684\u5185\u5b58\u6ca1\u6709\u521d\u59cb\u5316&#xff0c;\u6240\u4ee5\u5982 \u679c\u662f\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u5bf9\u8c61&#xff0c;\u9700\u8981\u4f7f\u7528new\u7684\u5b9a\u4e49\u8868\u8fbe\u5f0f\u8fdb\u884c\u663e\u793a\u8c03\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316<\/li>\n<\/ul>\n<h3>malloc\/free\u548cnew\/delete\u7684\u533a\u522b<\/h3>\n<p>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>malloc\u548cfree\u662f\u51fd\u6570&#xff0c;new\u548cdelete\u662f\u64cd\u4f5c\u7b26<\/li>\n<li>malloc\u7533\u8bf7\u7684\u7a7a\u95f4\u4e0d\u4f1a\u521d\u59cb\u5316&#xff0c;new\u53ef\u4ee5\u521d\u59cb\u5316<\/li>\n<li>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>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>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>\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>\u5185\u5b58\u7ba1\u7406 \u6808\u53c8\u53eb\u5806\u6808\u2013\u975e\u9759\u6001\u5c40\u90e8\u53d8\u91cf\/\u51fd\u6570\u53c2\u6570\/\u8fd4\u56de\u503c\u7b49\u7b49&#xff0c;\u6808\u662f\u5411\u4e0b\u589e\u957f\u7684\u5185\u5b58\u6620\u5c04\u6bb5\u662f\u9ad8\u6548\u7684I\/O\u6620\u5c04\u65b9\u5f0f&#xff0c;\u7528\u4e8e\u88c5\u8f7d\u4e00\u4e2a\u5171\u4eab\u7684\u52a8\u6001\u5185\u5b58\u5e93\u3002\u7528\u6237\u53ef\u4f7f\u7528\u7cfb\u7edf\u63a5\u53e3\u521b\u5efa\u5171\u4eab\u5185\u5b58&#xff0c;\u505a\u8fdb\u7a0b\u95f4\u901a\u4fe1\u5806\u7528\u4e8e\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a8\u6001\u5185\u5b58\u5206\u914d&#xff0c;\u5806\u5411\u4e0a\u589e\u957f\u6570\u636e\u6bb5\u2013\u5b58\u50a8\u5168\u5c40\u6570\u636e\u548c\u9759\u6001\u6570\u636e\u4ee3\u7801\u6bb5\u2013\u53ef\u6267\u884c\u7684\u4ee3\u7801\/\u53ea\u8bfb\u5e38\u91cf<br \/>\nc\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f<br \/>\nC\u8bed\u8a00\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\u5728C\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&amp;#xff0<\/p>\n","protected":false},"author":2,"featured_media":98885,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,190],"topic":[],"class_list":["post-98887","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>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\/98887.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"c++\u5185\u5b58\u7ba1\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5185\u5b58\u7ba1\u7406 \u6808\u53c8\u53eb\u5806\u6808\u2013\u975e\u9759\u6001\u5c40\u90e8\u53d8\u91cf\/\u51fd\u6570\u53c2\u6570\/\u8fd4\u56de\u503c\u7b49\u7b49&#xff0c;\u6808\u662f\u5411\u4e0b\u589e\u957f\u7684\u5185\u5b58\u6620\u5c04\u6bb5\u662f\u9ad8\u6548\u7684I\/O\u6620\u5c04\u65b9\u5f0f&#xff0c;\u7528\u4e8e\u88c5\u8f7d\u4e00\u4e2a\u5171\u4eab\u7684\u52a8\u6001\u5185\u5b58\u5e93\u3002\u7528\u6237\u53ef\u4f7f\u7528\u7cfb\u7edf\u63a5\u53e3\u521b\u5efa\u5171\u4eab\u5185\u5b58&#xff0c;\u505a\u8fdb\u7a0b\u95f4\u901a\u4fe1\u5806\u7528\u4e8e\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a8\u6001\u5185\u5b58\u5206\u914d&#xff0c;\u5806\u5411\u4e0a\u589e\u957f\u6570\u636e\u6bb5\u2013\u5b58\u50a8\u5168\u5c40\u6570\u636e\u548c\u9759\u6001\u6570\u636e\u4ee3\u7801\u6bb5\u2013\u53ef\u6267\u884c\u7684\u4ee3\u7801\/\u53ea\u8bfb\u5e38\u91cf c\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f C\u8bed\u8a00\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\u5728C\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&amp;#xff0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/98887.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-01T08:01:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260901080145-6a96866941c85.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=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/98887.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/98887.html\",\"name\":\"c++\u5185\u5b58\u7ba1\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-01T08:01:46+00:00\",\"dateModified\":\"2026-09-01T08:01:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/98887.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/98887.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/98887.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"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":"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\/98887.html","og_locale":"zh_CN","og_type":"article","og_title":"c++\u5185\u5b58\u7ba1\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5185\u5b58\u7ba1\u7406 \u6808\u53c8\u53eb\u5806\u6808\u2013\u975e\u9759\u6001\u5c40\u90e8\u53d8\u91cf\/\u51fd\u6570\u53c2\u6570\/\u8fd4\u56de\u503c\u7b49\u7b49&#xff0c;\u6808\u662f\u5411\u4e0b\u589e\u957f\u7684\u5185\u5b58\u6620\u5c04\u6bb5\u662f\u9ad8\u6548\u7684I\/O\u6620\u5c04\u65b9\u5f0f&#xff0c;\u7528\u4e8e\u88c5\u8f7d\u4e00\u4e2a\u5171\u4eab\u7684\u52a8\u6001\u5185\u5b58\u5e93\u3002\u7528\u6237\u53ef\u4f7f\u7528\u7cfb\u7edf\u63a5\u53e3\u521b\u5efa\u5171\u4eab\u5185\u5b58&#xff0c;\u505a\u8fdb\u7a0b\u95f4\u901a\u4fe1\u5806\u7528\u4e8e\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a8\u6001\u5185\u5b58\u5206\u914d&#xff0c;\u5806\u5411\u4e0a\u589e\u957f\u6570\u636e\u6bb5\u2013\u5b58\u50a8\u5168\u5c40\u6570\u636e\u548c\u9759\u6001\u6570\u636e\u4ee3\u7801\u6bb5\u2013\u53ef\u6267\u884c\u7684\u4ee3\u7801\/\u53ea\u8bfb\u5e38\u91cf c\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f C\u8bed\u8a00\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\u5728C\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&amp;#xff0","og_url":"https:\/\/www.wsisp.com\/helps\/98887.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-01T08:01:46+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260901080145-6a96866941c85.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/98887.html","url":"https:\/\/www.wsisp.com\/helps\/98887.html","name":"c++\u5185\u5b58\u7ba1\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-01T08:01:46+00:00","dateModified":"2026-09-01T08:01:46+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/98887.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/98887.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/98887.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"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\/98887","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=98887"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/98887\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/98885"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=98887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=98887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=98887"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=98887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}