{"id":2421,"date":"2024-07-20T03:20:52","date_gmt":"2024-07-19T19:20:52","guid":{"rendered":"https:\/\/courtship.top\/?p=2421"},"modified":"2024-07-20T03:21:50","modified_gmt":"2024-07-19T19:21:50","slug":"%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f%e5%87%a0%e5%88%99","status":"publish","type":"post","link":"https:\/\/courtship.top\/index.php\/2024\/07\/20\/%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f%e5%87%a0%e5%88%99\/","title":{"rendered":"\u5185\u5b58\u6cc4\u6f0f\u51e0\u5219"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u6982\u5ff5<\/h2>\n\n\n\n<p><strong>\u5185\u5b58\u6cc4\u6f0f\uff0c\u662f\u6307\u5728\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u52a8\u6001\u7533\u8bf7\u7684\u3001\u5806\u4e0a\u7684\u5185\u5b58 \u7531\u4e8e\u67d0\u79cd\u539f\u56e0\u3001\u5728\u4f7f\u7528\u540e\u6ca1\u6709\u88ab\u91ca\u653e\uff0c\u8fdb\u800c\u9020\u6210\u5185\u5b58\u7684\u6d6a\u8d39\u3002<\/strong><\/p>\n\n\n\n<p>\u5c11\u90e8\u5206\u7684\u5185\u5b58\u6cc4\u6f0f\u4e0d\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u7684\u6b63\u5e38\u8fd0\u884c\uff0c\u4e0d\u8fc7\u5982\u679c\u662f\u6301\u7eed\u7684\u5185\u5b58\u6cc4\u6f0f\u4f1a\u8017\u5149\u7cfb\u7edf\u5185\u5b58\uff0c\u6700\u7ec8\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5361\u6b7b\u751a\u81f3\u7cfb\u7edf\u5d29\u6e83\u3002\u4e3a\u4e86\u907f\u514d\u7cfb\u7edf\u5d29\u6e83\uff0c\u5728\u65e0\u6cd5\u7533\u8bf7\u5230\u5185\u5b58\u7684\u65f6\u5019\uff0c\u8981\u679c\u65ad\u8c03\u7528exit()\u51fd\u6570\u4e3b\u52a8\u6740\u6b7b\u8fdb\u7a0b\uff0c\u800c\u4e0d\u662f\u8bd5\u56fe\u633d\u6551\u8fd9\u4e2a\u8fdb\u7a0b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u5bdf\u89c9<\/h2>\n\n\n\n<p>\u5982\u679c\u7a0b\u5e8f\u5728\u6b63\u5e38\u5730\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u5360\u7528\u7684\u5185\u5b58\u968f\u7740\u65f6\u95f4\u63a8\u79fb\u4e0d\u65ad\u589e\u957f\uff0c\u4e00\u822c\u5c31\u8bf4\u660e\u5b58\u5728\u5185\u5b58\u6cc4\u6f0f\u7684\u60c5\u51b5\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e13\u95e8\u7684\u5de5\u5177\u6765\u68c0\u6d4b\u7a0b\u5e8f\u4e2d\u7684\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ea7\u751f\u7684\u539f\u56e0\u51e0\u5219<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">malloc\/new\u7533\u8bf7\u7684\u5185\u5b58\u6ca1\u6709\u4e3b\u52a8\u91ca\u653e<\/h3>\n\n\n\n<p>\u4f7f\u7528 malloc \u7533\u8bf7\u7684\u5185\u5b58\u8981\u4e3b\u52a8\u8c03\u7528 free\uff0cnew \u7533\u8bf7\u7684\u5185\u5b58\u8981\u4e3b\u52a8\u8c03\u7528 delete\uff0c\u5426\u5219\u5c31\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002\u4f8b\u5982\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684\u5185\u5b58 ptr \u5728\u7533\u8bf7\u540e\u6ca1\u6709\u88ab\u91ca\u653e\u5c31\u9020\u6210\u4e86\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main(){\n    void* ptr = malloc(1);\n    \/\/ use ptr ...\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528free\u91ca\u653enew\u7533\u8bf7\u7684\u5185\u5b58<\/h3>\n\n\n\n<p>malloc\/free\u4ee5\u53canew\/delete\u5fc5\u987b\u662f\u5404\u81ea\u6210\u5bf9\u51fa\u73b0\uff0c\u5982\u679c\u6df7\u7528\uff0c\u5c31\u4f1a\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u60c5\u51b5\u51fa\u73b0\u3002\u53e6\u5916\uff0c\u5982\u679c\u7528delete\u91ca\u653evoid\u6307\u9488\u6307\u5411\u7684\u5bf9\u8c61\u540c\u6837\u4e5f\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u9732\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528delete\u53bb\u5220\u9664\u6570\u7ec4<\/h3>\n\n\n\n<p>\u4f7f\u7528 new \u7533\u8bf7\u7684\u6570\u7ec4\uff0c\u91ca\u653e\u7684\u65f6\u5019\u8981\u7528 delete[] \u5220\u9664\uff0c\u5982\u679c\u9519\u8bef\u5730\u4f7f\u7528 delete \u5220\u9664\uff0c\u5c31\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main(){\n    int* ptr = new int&#91;2];\n    \/\/ usr ptr ...\n    \/\/ delete ptr; \/\/ \u9519\u8bef\uff01\u91ca\u653e\u6570\u7ec4\u8981\u7528 delete&#91;]\n    delete&#91;] ptr; \/\/ \u6b63\u786e\uff01\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u7c7b\u7684\u6790\u6784\u51fd\u6570\u6ca1\u6709\u5b9a\u4e49\u4e3a\u865a\u51fd\u6570<\/h3>\n\n\n\n<p>\u5f53\u57fa\u7c7b\u6307\u9488\u6307\u5411\u5b50\u7c7b\u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u57fa\u7c7b\u7684\u6790\u6784\u51fd\u6570\u4e0d\u662fvirtual\uff0c\u90a3\u4e48\u5b50\u7c7b\u7684\u6790\u6784\u51fd\u6570\u5c06\u4e0d\u4f1a\u88ab\u8c03\u7528\uff0c\u5b50\u7c7b\u7684\u8d44\u6e90\u6ca1\u6709\u6b63\u786e\u662f\u91ca\u653e\uff0c\u56e0\u6b64\u9020\u6210\u5185\u5b58\u6cc4\u9732\u3002\u4f8b\u5982\u4e0b\u9762\u4ee3\u7801\u4e2d\u6790\u6784\u51fd\u6570 ~A() \u4e0d\u662fvirtual\uff0c\u5728\u8c03\u7528 delete pa \u7684\u65f6\u5019\u5c31\u4e0d\u4f1a\u8c03\u7528\u5b50\u7c7b B \u7684\u6790\u6784\u51fd\u6570\uff0c\u8be5\u5bf9\u8c61\u4e2d\u5b50\u7c7b B \u4e2d\u7684\u5185\u5b58\u65e0\u6cd5\u88ab\u91ca\u653e\u5e72\u51c0\u3002<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class A\n{\npublic:\n\tA(){}\n\t~A(){}\n}\n \nclass B : public A \n{\npublic:\n\tB(){}\n\t~B(){}\nprivate:\n\tint num;\n}\n \nvoid main(){\n\tA* pa = new B();\n\tdelete pa;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u8c28\u614e\u4f7f\u7528\u52a8\u6001\u5185\u5b58<\/h3>\n\n\n\n<p>\u5728\u7f16\u5199\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u5bf9\u52a8\u6001\u5185\u5b58\u4fdd\u6301\u8b66\u60d5\uff0c\u4fdd\u8bc1\u6bcf\u4e00\u5757\u513f\u7533\u8bf7\u7684\u5185\u5b58\u90fd\u8981\u5f97\u5230\u91ca\u653e\u3002\u7279\u522b\u662f\u5728\u6bcf\u4e2a return \u4e4b\u524d\uff0c\u8981\u60f3\u4e00\u60f3\u662f\u5426\u8fd8\u6709\u5185\u5b58\u6ca1\u6709\u88ab\u91ca\u653e\uff0c\u5982\u679c\u8fd9\u91cc\u4e0d\u91ca\u653e\uff0c\u5728\u5176\u4ed6\u5730\u65b9\u662f\u5426\u4f1a\u6b63\u5e38\u91ca\u653e\u3002<\/p>\n\n\n\n<p>\u8fd9\u662f\u4e00\u79cd\u9760\u8111\u888b\u7684\u65b9\u5f0f\uff0c\u9700\u8981\u7f16\u5199\u4ee3\u7801\u7684\u65f6\u5019\u65f6\u523b\u4fdd\u6301\u654f\u611f\uff0c\u4f46\u662f\u8111\u888b\u5f80\u5f80\u662f\u4e0d\u53ef\u9760\u7684\u3002\u6700\u597d\u9009\u7528\u5176\u4ed6\u7684\u65b9\u5f0f\u6765\u4fdd\u969c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528RAII<code>\u3002<\/code><\/h3>\n\n\n\n<p>RAII\uff0c\u5168\u79f0<strong>\u8d44\u6e90\u83b7\u53d6\u5373\u521d\u59cb\u5316<\/strong>\uff08\u82f1\u8bed\uff1aResource Acquisition Is Initialization\uff09\uff0c\u901a\u8fc7\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u5b9e\u73b0\u8d44\u6e90\u83b7\u53d6\uff0c\u901a\u8fc7\u5bf9\u8c61\u7684\u9500\u6bc1\u5b9e\u73b0\u8d44\u6e90\u7684\u91ca\u653e\uff0c\u6211\u4eec\u6240\u8bf4\u7684\u8d44\u6e90\u5c31\u662f\u52a8\u6001\u5185\u5b58\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7 MemBlock \u7c7b\u7684\u6784\u9020\u51fd\u6570\u5206\u914d\u5185\u5b58\uff0c\u901a\u8fc7\u6790\u6784\u51fd\u6570\u91ca\u653e\u5185\u5b58\uff0c\u5728\u9700\u8981\u4f7f\u7528\u52a8\u6001\u5185\u5b58\u7684\u5730\u65b9\u53ea\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a MemBlock \u5bf9\u8c61 buff\uff0c\u901a\u8fc7\u6210\u5458\u51fd\u6570 get() \u83b7\u53d6\u5185\u5b58\u5730\u5740\uff0c\u4f7f\u7528\u4e4b\u540e\u65e0\u9700\u624b\u52a8\u91ca\u653e\u5185\u5b58\uff0c\u5728 buff \u79bb\u5f00\u4f5c\u7528\u57df\u7684\u65f6\u5019\uff0cbuff \u4f1a\u88ab\u81ea\u52a8\u91ca\u653e\uff08\u8c03\u7528\u6790\u6784\u51fd\u6570\uff09\uff0c\u5728\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528 free \u91ca\u653e buff \u6240\u6301\u6709\u7684\u5185\u5b58\u3002\u5982\u679c\u5728\u6240\u6709\u9700\u8981\u4f7f\u7528\u5185\u5b58\u7684\u5730\u65b9\u90fd\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ea\u8981\u4fdd\u8bc1 MemBlock \u5bf9\u8c61\u80fd\u88ab\u6790\u6784\uff0c\u5c31\u4e0d\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MemBlock\n{\npublic:\nMemBlock(size_t size)\n{\n_ptr = malloc(size);\n}\n~MemBlock()\n{\nfree(_ptr);\n}\npublic:\nvoid* ptr()\n{\nreturn _ptr;\n}\nprotected:\nvoid* _ptr;\n};\nvoid main(){\nMemBlock buff(1024);\nmemset(buff.ptr(), 0x00, 1024);\n\/\/ \u4f7f\u7528\u5185\u5b58\u540e\u65e0\u9700\u4e3b\u52a8\u91ca\u653e\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u667a\u80fd\u6307\u9488<\/h3>\n\n\n\n<p><strong>\u4f7f\u7528\u667a\u80fd\u6307\u9488\u76f4\u63a5\u7ba1\u7406\u52a8\u6001\u5185\u5b58<\/strong>\uff0c\u5728\u4f7f\u7528\u4e4b\u540e\u4e0d\u9700\u8981\u624b\u52a8\u91ca\u653e\uff0c\u5f53\u8fd9\u6bb5\u5185\u5b58\u4e0d\u518d\u88ab\u5f15\u7528\u7684\u65f6\u5019\uff0c\u8fd9\u6bb5\u5185\u5b58\u4f1a\u88ab\u8c03\u7528 free \u51fd\u6570\u6765\u91ca\u653e\uff0cfree\u51fd\u6570\u662f\u4f5c\u4e3a\u81ea\u5b9a\u4e49\u7684\u91ca\u653e\u51fd\u6570\u4f20\u7ed9\u667a\u80fd\u6307\u9488\u7684\uff0c\u5982\u679c\u662f\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4e0d\u9700\u8981\u4f20\u5165\u91ca\u653e\u51fd\u6570\uff0c\u4f1a\u9ed8\u8ba4\u8c03\u7528\u7c7b\u578b\u7684\u6790\u6784\u51fd\u6570\u6765\u91ca\u653e\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u5ff5 \u5185\u5b58\u6cc4\u6f0f\uff0c\u662f\u6307\u5728\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u52a8\u6001\u7533\u8bf7\u7684\u3001\u5806\u4e0a\u7684\u5185\u5b58 \u7531\u4e8e\u67d0\u79cd\u539f\u56e0\u3001\u5728\u4f7f\u7528\u540e\u6ca1\u6709\u88ab\u91ca\u653e\uff0c\u8fdb\u800c\u9020\u6210\u5185\u5b58\u7684\u6d6a\u8d39\u3002 \u5c11\u90e8\u5206\u7684\u5185\u5b58\u6cc4\u6f0f\u4e0d &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[22],"tags":[],"class_list":["post-2421","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/comments?post=2421"}],"version-history":[{"count":8,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2421\/revisions"}],"predecessor-version":[{"id":2429,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2421\/revisions\/2429"}],"wp:attachment":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/media?parent=2421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/categories?post=2421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/tags?post=2421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}