{"id":306442,"date":"2024-05-20T20:10:15","date_gmt":"2024-05-20T12:10:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/306442.html"},"modified":"2024-05-20T20:10:39","modified_gmt":"2024-05-20T12:10:39","slug":"c-%e4%b8%ad-main-%e4%b8%bb%e5%87%bd%e6%95%b0%e6%89%a7%e8%a1%8c%e5%ae%8c%e6%af%95%e5%90%8e%ef%bc%8c%e4%bc%9a%e6%89%a7%e8%a1%8c%e4%bb%80%e4%b9%88%e4%bb%a3%e7%a0%81-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/306442.html","title":{"rendered":"c   \u4e2d main \u4e3b\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u4f1a\u6267\u884c\u4ec0\u4e48\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26222827\/d562f446-cb12-44ff-be58-b95c77d79c30.webp\" alt=\"c   \u4e2d main \u4e3b\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u4f1a\u6267\u884c\u4ec0\u4e48\u4ee3\u7801\" \/><\/p>\n<p><p>C\u8bed\u8a00\u4e2d\u7684<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n<\/code>\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u901a\u5e38\u4f1a\u6267\u884c\u7531C\u8fd0\u884c\u65f6\u5e93\u63d0\u4f9b\u7684\u6e05\u7406\u4ee3\u7801\uff0c\u7136\u540e\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u63a5\u7ba1<strong>\u63a7\u5236\u6743<\/strong>\u3001<strong>\u8fd4\u56de\u64cd\u4f5c\u7cfb\u7edf\u9519\u8bef\u7801<\/strong>\u3001<strong>\u8fdb\u884c\u8fdb\u7a0b\u8d44\u6e90\u56de\u6536<\/strong>\u3002\u8fd9\u6bb5\u6e05\u7406\u4ee3\u7801\u901a\u5e38\u8d1f\u8d23\u5173\u95ed\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u8c03\u7528\u5404\u79cd\u7ec8\u6b62\u5904\u7406\u51fd\u6570\uff08\u6bd4\u5982\u6ce8\u518c\u7684<code>atexit<\/code>\u51fd\u6570\uff09\u3001\u8fdb\u884c\u5168\u5c40\u5bf9\u8c61\u7684\u6790\u6784\uff08\u5bf9\u4e8eC++\uff09\uff0c\u6700\u7ec8\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u8868\u793a\u7a0b\u5e8f\u7684\u9000\u51fa\u7801\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><p>\u7279\u522b\u5730\uff0c<code>main<\/code>\u51fd\u6570\u4e2d\u7684<code>return<\/code>\u8bed\u53e5\u6240\u8fd4\u56de\u7684\u503c\uff0c\u4f1a\u88ab\u4f20\u9012\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u4f5c\u4e3a\u7a0b\u5e8f\u7684\u9000\u51fa\u72b6\u6001\u3002\u8fd9\u4e2a\u503c\u53ef\u4ee5\u88ab\u5176\u4ed6\u7a0b\u5e8f\u6216\u8005\u811a\u672c\u7528\u6765\u5224\u65ad\u8be5\u7a0b\u5e8f\u662f\u5426\u6210\u529f\u6267\u884c\u6216\u8005\u6267\u884c\u65f6\u662f\u5426\u9047\u5230\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8fdb\u7a0b\u8d44\u6e90\u56de\u6536<\/h3>\n<\/p>\n<p><p>\u64cd\u4f5c\u7cfb\u7edf\u5728<code>main<\/code>\u6267\u884c\u5b8c\u6bd5\u540e\u4f1a\u56de\u6536\u8fdb\u7a0b\u5360\u7528\u7684\u6240\u6709\u8d44\u6e90\u3002\u8fd9\u5305\u62ec\u5185\u5b58\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u4ee5\u53ca\u4efb\u4f55\u8fdb\u7a0b\u7ea7\u522b\u7684\u9501\u6216\u8d44\u6e90\u3002\u64cd\u4f5c\u7cfb\u7edf\u56de\u6536\u8d44\u6e90\u662f\u4e3a\u4e86\u786e\u4fdd\u7cfb\u7edf\u7684\u7a33\u5b9a\u8fd0\u884c\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8fd4\u56de\u64cd\u4f5c\u7cfb\u7edf\u9519\u8bef\u7801<\/h3>\n<\/p>\n<p><p>\u8fd4\u56de\u9519\u8bef\u7801\u662f<code>main<\/code>\u51fd\u6570\u6267\u884c\u5b8c\u540e\u53e6\u4e00\u9879\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u9519\u8bef\u7801\u63d0\u4f9b\u4e86\u7a0b\u5e8f\u6267\u884c\u72b6\u6001\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u88ab\u64cd\u4f5c\u7cfb\u7edf\u6216\u8c03\u7528\u8be5\u7a0b\u5e8f\u7684\u5176\u4ed6\u7a0b\u5e8f\uff08\u4f8b\u5982shell\uff09\u8fdb\u884c\u8bfb\u53d6\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6807\u51c6\u5e93\u6e05\u7406\u5de5\u4f5c<\/h3>\n<\/p>\n<p><p>C\u8bed\u8a00\u7684\u6807\u51c6\u5e93\uff08libc\uff09\u4e00\u822c\u4f1a\u63d0\u4f9b\u4e00\u4e9b\u81ea\u52a8\u6267\u884c\u7684\u6e05\u7406\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u6807\u51c6I\/O\u5e93\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u4f1a\u81ea\u52a8\u5173\u95ed\u6240\u6709\u6253\u5f00\u7684\u6807\u51c6I\/O\u6d41\u3002\u5982\u679c\u7a0b\u5e8f\u5458\u6709\u4f7f\u7528\u7c7b\u4f3c<code>malloc<\/code>\u7684\u5185\u5b58\u5206\u914d\u51fd\u6570\u5206\u914d\u8d44\u6e90\uff0c\u7406\u5e94\u5728\u7a0b\u5e8f\u9000\u51fa\u524d\u660e\u786e\u7528<code>free<\/code>\u91ca\u653e\uff0c\u4f46\u64cd\u4f5c\u7cfb\u7edf\u5728\u6700\u7ec8\u8fd8\u662f\u4f1a\u63a5\u7ba1\u5e76\u91ca\u653e\u6389\u8fdb\u7a0b\u6240\u5360\u7528\u7684\u6240\u6709\u5185\u5b58\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8fd0\u884catexit\u6ce8\u518c\u7684\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u6267\u884c\u671f\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>atexit()<\/code>\u51fd\u6570\u6ce8\u518c\u82e5\u5e72\u4e2a\u9700\u8981\u5728<code>main<\/code>\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u8c03\u7528\u7684\u51fd\u6570\u3002\u8fd9\u4e9b\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u8d44\u6e90\u6e05\u7406\u3001\u65e5\u5fd7\u8bb0\u5f55\u7b49\u3002\u5f53<code>main<\/code>\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u8fd9\u4e9b\u901a\u8fc7<code>atexit<\/code>\u6ce8\u518c\u7684\u51fd\u6570\u5c06\u6309\u7167\u6ce8\u518c\u7684\u9006\u5e8f\u4f9d\u6b21\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5168\u5c40\u5bf9\u8c61\u7684\u6790\u6784\uff08\u4ec5C++\uff09<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u95ee\u9898\u4ec5\u63d0\u53ca\u4e86C\u8bed\u8a00\uff0c\u4f46\u5728C++\u4e2d\uff0c<code>main<\/code>\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u4e4b\u540e\uff0c\u4f1a\u7531C++\u8fd0\u884c\u65f6\u73af\u5883\u8c03\u7528\u5168\u5c40\u5bf9\u8c61\u7684\u6790\u6784\u51fd\u6570\u3002\u8fd9\u786e\u4fdd\u4e86\u5177\u6709\u9759\u6001\u5b58\u50a8\u671f\u7684\u5bf9\u8c61\uff08\u4f8b\u5982\u5168\u5c40\u5bf9\u8c61\u3001\u9759\u6001\u53d8\u91cf\uff09\u80fd\u591f\u6309\u6b63\u786e\u7684\u987a\u5e8f\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: C\u8bed\u8a00\u4e2d\uff0cmain\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4f1a\u6267\u884c\u54ea\u4e9b\u4ee3\u7801\uff1f<\/strong><\/p>\n<p>A1: \u5728C\u8bed\u8a00\u4e2d\uff0cmain\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u9664\u975e\u7a0b\u5e8f\u660e\u786e\u7ec8\u6b62\uff0c\u5426\u5219\u4f1a\u7ee7\u7eed\u6267\u884c\u7a0b\u5e8f\u7684\u5269\u4f59\u90e8\u5206\u3002\u8fd9\u4e9b\u5269\u4f59\u90e8\u5206\u901a\u5e38\u5305\u62ec\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u3001\u9759\u6001\u5168\u5c40\u53d8\u91cf\u7684\u9500\u6bc1\u3001\u52a8\u6001\u5185\u5b58\u7684\u91ca\u653e\u7b49\u64cd\u4f5c\u3002<\/p>\n<p><strong>Q2: main\u51fd\u6570\u6267\u884c\u7ed3\u675f\u540e\uff0c\u7a0b\u5e8f\u4f1a\u7ee7\u7eed\u6267\u884c\u54ea\u4e9b\u4ee3\u7801\uff1f<\/strong><\/p>\n<p>A2: \u5728C\u8bed\u8a00\u4e2d\uff0cmain\u51fd\u6570\u6267\u884c\u7ed3\u675f\u540e\uff0c\u7a0b\u5e8f\u4f1a\u7ee7\u7eed\u6267\u884c\u5168\u5c40\u4f5c\u7528\u57df\u4e0b\u7684\u5176\u4ed6\u51fd\u6570\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u662f\u7531main\u51fd\u6570\u8c03\u7528\u7684\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u7531\u5176\u4ed6\u51fd\u6570\u76f4\u63a5\u6216\u95f4\u63a5\u8c03\u7528\u7684\u51fd\u6570\u3002<\/p>\n<p><strong>Q3: \u5f53main\u51fd\u6570\u6267\u884c\u5b8c\u6210\u540e\uff0c\u7a0b\u5e8f\u8fd8\u4f1a\u7ee7\u7eed\u6267\u884c\u54ea\u4e9b\u4ee3\u7801\uff1f<\/strong><\/p>\n<p>A3: \u5728C\u8bed\u8a00\u4e2d\uff0cmain\u51fd\u6570\u6267\u884c\u5b8c\u6210\u540e\uff0c\u7a0b\u5e8f\u8fd8\u4f1a\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\uff0c\u4f8b\u5982\u5173\u95ed\u6253\u5f00\u7684\u6587\u4ef6\u3001\u91ca\u653e\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u7684\u8d44\u6e90\u7b49\u3002\u6b64\u5916\uff0c\u7a0b\u5e8f\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7cfb\u7edf\u63d0\u4f9b\u7684exit\u51fd\u6570\u6765\u663e\u5f0f\u5730\u7ec8\u6b62\u7a0b\u5e8f\u7684\u6267\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"C\u8bed\u8a00\u4e2d\u7684mAIn\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u901a\u5e38\u4f1a\u6267\u884c\u7531C\u8fd0\u884c\u65f6\u5e93\u63d0\u4f9b\u7684\u6e05\u7406\u4ee3\u7801\uff0c\u7136\u540e\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u63a5\u7ba1\u63a7\u5236\u6743\u3001\u8fd4\u56de\u64cd\u4f5c\u7cfb\u7edf [&hellip;]","protected":false},"author":3,"featured_media":306469,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/306442"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=306442"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/306442\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/306469"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=306442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=306442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=306442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}