{"id":89669,"date":"2019-08-02T10:44:31","date_gmt":"2019-08-02T02:44:31","guid":{"rendered":"http:\/\/4563.org\/?p=89669"},"modified":"2019-08-02T10:44:31","modified_gmt":"2019-08-02T02:44:31","slug":"%e5%85%b3%e4%ba%8e-c%e6%8c%87%e9%92%88%e6%95%b0%e7%bb%84%e9%95%bf%e5%ba%a6%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=89669","title":{"rendered":"\u5173\u4e8e c++\u6307\u9488\u6570\u7ec4\u957f\u5ea6\u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e c++\u6307\u9488\u6570\u7ec4\u957f\u5ea6\u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Huelse <\/span>  <span><i><\/i> 55<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u60c5\u51b5\u5982\u4e0b\uff1a<\/p>\n<pre><code>#include &lt;malloc.h&gt;  int size = 7; double *a = new double[size]; cout &lt;&lt; malloc_usable_size(a) \/ sizeof(*a) &lt;&lt; endl; \/\/\u8f93\u51fa\u7ed3\u679c\u4e3a 7  int size = 8; double *a = new double[size]; cout &lt;&lt; malloc_usable_size(a) \/ sizeof(*a) &lt;&lt; endl; \/\/\u8f93\u51fa\u7ed3\u679c\u4e3a 9  \/\/sizeof(a)=sizeof(*a)=sizeof(double)=8 <\/code><\/pre>\n<p>\u5f53 size=8 \u6216 1024 \u5176\u4ed6\u5076\u6570\u65f6,malloc_usable_size \u4f1a\u591a\u4e00\u4e2a size<\/p>\n<p>\u4e5f\u5c31\u662f\u6570\u7ec4\u957f\u5ea6\u4e3a\u5076\u6570\u60c5\u51b5\u4e0b\u8f93\u51fa\u7ed3\u679c\u4f1a\u6bd4\u5b9e\u9645\u7684\u52a0 1<\/p>\n<p>\u6211\u60f3\u95ee\u4e0b\u8fd9\u4e2a\u8981\u600e\u4e48\u5904\u7406\u624d\u59cb\u7ec8\u6b63\u786e\uff1f<\/p>\n<p>\u6216\u8005\u6709\u6ca1\u6709\u5176\u4ed6\u66f4\u597d\u7684\u83b7\u53d6\u6307\u9488\u6570\u7ec4\u957f\u5ea6\u7684\u65b9\u6cd5\uff1f<\/p>\n<p>\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>17<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"587156\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codehz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b54\u6848\u662f\u4e0d\u80fd\u83b7\u5f97\u957f\u5ea6\uff08<br \/>\u4f60\u5fc5\u987b\u7528\u4e2a\u53d8\u91cf\u53bb\u8bb0\u5f55                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587157\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : inhzus <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             malloc_usable_size() returns the number of bytes available in the dynamically allocated buffer ptr, which may be greater than the requested size (but is guaranteed to be at least as large, if the request was successful). Typically, you should store the requested allocation size rather than use this function.<\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u7684\u6587\u6863\u6709\u63d0\u5230\u8fd9\u70b9\u7684\u3002\u5efa\u8bae\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6700\u597d\u770b\u770b\u5b98\u65b9\u6587\u6863\u7684~<\/p>\n<p>\u56de\u5230\u83b7\u53d6\u6307\u9488\u6307\u5411\u6570\u7ec4\u7684\u957f\u5ea6\u8fd9\u4e2a\u95ee\u9898\uff0c\u662f\u4e0d\u80fd\u76f4\u63a5\u5f97\u5230\u7684\uff08\u6709\u4ec0\u4e48\u5947\u6280\u6deb\u5de7\u5c31\u4e0d\u6653\u5f97\u4e86\uff09\u3002\u5efa\u8bae\u7528 std::array \u6216 std::vector                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587158\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Huelse <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @codehz #1 <br \/>@inhzus #2 <br \/>\u731c\u5230\u4e86\u8fd9\u4e2a\u7ed3\u679c\uff0c\u8c22\u8c22\u4f60\u4eec\u7684\u56de\u7b54                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587159\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : crclz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             c \u8bed\u8a00\u662f\u8bbe\u8ba1\u7684\u5f88\u7b80\u7ec3\u7684\u8bed\u8a00\uff0c\u5b83\u662f\u4e2a\u4eba\u8bbe\u8ba1\u7684\uff0c\u5e76\u975e\u7ec4\u7ec7\u8bbe\u8ba1\u7684\u3002c \u8bed\u8a00\u5f88\u591a\u5730\u65b9\u6309\u7167\u7ea6\u5b9a\u6765\u8bbe\u8ba1\u7684\uff0c\u53ef\u80fd\u5728\u4e3b\u529b\u8bed\u8a00\u662f java \u6216\u8005 c#\u7684\u4eba\u770b\u6765\u6bd4\u8f83\u4e0d\u7f8e\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u8fd9\u662f\u7b80\u6d01\u7684\u7f8e\u3002<br \/>\u4f8b\u5982\uff1a\u5b57\u7b26\u4e32\u4ee5&#8217;\u0000&#8217;\u7ed3\u5c3e\u3002\u4e0e\u5b57\u7b26\u4e32\u64cd\u4f5c\u76f8\u5173\u7684\u5e93\u51fd\u6570\u4e5f\u90fd\u662f\u57fa\u4e8e\u8fd9\u4e2a\u5047\u8bbe\u3002<br \/>\u518d\u4f8b\u5982\uff0c\u5e93\u51fd\u6570\u3001\u53d8\u91cf\u547d\u540d\u90fd\u6709\u5927\u91cf\u7f29\u5199\uff0c\u4f8b\u5982 getchar, fopen\uff0c\u800c\u4e0d\u662f OpenFile()\u8fd9\u79cd\u98ce\u683c\u3002<br \/>\u800c\u6570\u7ec4\u7684\u7ea6\u5b9a\uff0c\u5c31\u662f\u5c06\u6570\u7ec4\u4e0e\u6307\u9488\u7b49\u540c\u8d77\u6765\u7406\u89e3\u3002\u5982\u679c\u9700\u8981\u8bb0\u5f55\u957f\u5ea6\uff0c\u8bf7\u5355\u72ec\u62ff\u4e00\u4e2a\u53d8\u91cf\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587160\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hobochen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @crclz ISO\/IEC 9899:2011 \u5fc3\u91cc\u4e00\u53e5 \u4f60\u77e5\u9053\u6211\u6709\u591a\u52aa\u529b\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587161\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : crclz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @hobochen c \u8bed\u8a00\u662f\u4e2a\u4eba\u8bbe\u8ba1\u7684\u800c\u975e\u7ec4\u7ec7\u8bbe\u8ba1\u7684\uff0c\u8fd9\u53e5\u8bdd\uff08\u5927\u610f\uff09\uff0c\u6211\u662f\u4ece\u300a\u6df1\u5165\u7406\u89e3\u8ba1\u7b97\u673a\u539f\u7406\u300b\u91cc\u9762\u770b\u6765\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587162\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leido <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u54ea\u672c\u4e66\u544a\u8bc9\u4f60\u5806\u5185\u5b58\u53ef\u4ee5\u8fd9\u6837\u7b97\u7a7a\u95f4\uff1f\uff1f<br \/>\u6808\u4e0a\u624d\u53ef\u4ee5\uff0c\u524d\u63d0\u662f\u5728\u7533\u8bf7\u6808\u7a7a\u95f4\u7684\u51fd\u6570\u5185                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587163\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : laminux29 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8001\u54e5\u662f Java \u7528\u591a\u4e86\u5427&#8230;\u5c31\u7b97 malloc_usable_size \u8fd9\u51fd\u6570\u80fd\u7ed9\u51fa\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u4e07\u4e00\u7f16\u8bd1\u5668\u628a\u5b83\u5f53\u51fd\u6570\u6765\u7f16\u8bd1\uff0c\u90a3\u4e48\u5b83\u7684\u6027\u80fd\u4e5f\u6709\u53ef\u80fd\u6bd4\u81ea\u5df1\u7528\u53d8\u91cf\u8bb0\u5f55\u6162\u5f97\u591a\u554a\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587164\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dacapoday <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @crclz \u7f29\u5199\u8fd9\u4e2a\u662f\u65f6\u4ee3\u6240\u9650\uff0c\u65e9\u671f\u7684\u7f16\u8bd1\u5668\u4e0d\u652f\u6301\u8d85\u8fc7 8 \u4f4d\u7684 token\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587165\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ipwx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8c01\u8bf4\u4f60\u7533\u8bf7\u591a\u5c11\u5185\u5b58\uff0cmalloc \u5c31\u4e00\u5b9a\u5212\u591a\u5c11\u5185\u5b58\u7ed9\u4f60\u7684\uff1f\u4e3a\u4e86\u89e3\u51b3\u5185\u5b58\u788e\u7247\uff0cmalloc \u5f88\u53ef\u80fd\u4ee5\u4e00\u5b9a\u7a0b\u5ea6\u5730\u727a\u7272\u5185\u5b58\u4e3a\u4ee3\u4ef7\uff0c\u7ed9\u4f60\u5212\u4e00\u5757\u7a0d\u5fae\u5927\u4e00\u70b9\u4f46\u662f\u4e0d\u81f3\u4e8e\u5f15\u8d77\u5185\u5b58\u788e\u7247\u7684\u5185\u5b58\u7ed9\u4f60\u3002<\/p>\n<p>\u4e00\u4e2a\u7ecf\u5178\u7684\u5185\u5b58\u5206\u914d\u7b97\u6cd5\u662f https:\/\/en.wikipedia.org\/wiki\/Buddy_memory_allocation                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587166\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : FrankHB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @hobochen C2x \u90fd\u5feb\u51fa\u6765\u4e86\u8fd8 2011\uff0c\uff0c\uff0c<br \/>\u2026\u2026\u561b\uff0cN1570 \u4ee5\u540e\u7684 draft \u548c\u6b63\u5f0f\u51fa\u7248\u7684\u592a\u63a5\u8fd1\u7684\u5f97\u8981\u5bc6\u7801\uff08                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587167\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : FrankHB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u2026\u2026\u54e6\u8fd9\u4e2a\u662f C \u8279\u4e48\u3002<br \/>\u9001 LZ \u4e00\u53e5\u8bdd\uff1a<br \/>\u201c\u6041\u4e5f\u914d\u7528 new \uff1f\u201d<br \/>\u7406\u89e3\u597d\u4e86\u518d\u5012\u817e\uff0c\u5bf9\u8c01\u90fd\u597d\uff0c\u8c22\u8c22\u5408\u4f5c\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587168\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lomewcl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u662f\u5f88\u6e05\u695a malloc \u65cf\u76f8\u5173\u7684\u51fd\u6570\u80fd\u4e0d\u80fd\u7528\u5230 new \u4e0a\uff0c\u61d2\u5f97\u67e5\u4e86\u3002<br \/>\u5806\u5206\u914d\u7684\u5185\u5b58\u4ee5 chunk \u4e3a\u6700\u5c0f\u5355\u4f4d\uff0c\u6d89\u53ca\u5bf9\u9f50\u7b49\uff0c\u6bd4\u7533\u8bf7\u7684\u7a7a\u95f4\u5927\u4e00\u70b9\u662f\u5f88\u6b63\u5e38\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587169\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : geelaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @FrankHB #12 \u4e3b\u8fd9\u4e2a malloc_usable_size \u90fd\u4e0d\u662f C \u6216\u8005 C++ \u7684\u4e00\u90e8\u5206\uff08                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587170\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : augustheart <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u8981\u53bb\u8ba1\u7b97\u5806\u5185\u5b58\uff0c\u867d\u7136\u5176\u5b9e\u6709\u4e9b\u65b9\u6cd5\u53ef\u4ee5\uff0c\u4f46\u662f\u4e0d\u53ef\u9760\uff0c\u4e0d\u80fd\u5f97\u5230\u7cbe\u786e\u7684\u5185\u5b58\u3002\u56e0\u4e3a c \u5206\u914d\u5185\u5b58\u7684\u65f6\u5019\u5c31\u4e0d\u4f1a\u4e25\u683c\u6309\u7167\u7533\u8bf7\u7684\u957f\u5ea6\u5206\u914d\uff0c\u4f1a\u6709\u7b49\u4e8e\u6307\u9488\u957f\u5ea6\u7684\u5bf9\u9f50\u3002<br \/>\u53e6\u5916\uff0cc \u7684\u6570\u7ec4\u540e\u9762\u662f\u6709 0 \u5360\u4f4d\u7684\uff0c\u914d\u5408\u4e0a\u5bf9\u9f50\u7684\u6548\u679c\uff0c\u6240\u4ee5\u4f1a\u51fa\u73b0 size + 1 \u7684\u60c5\u51b5\uff08\u4e0d\u80fd\u7406\u89e3\u7684\u8bdd\u5c31\u8fc7\u5427\uff0c\u4e0d\u641e\u9006\u5411\u7684\u8bdd\u4e0d\u9700\u8981\u7406\u89e3\u8fd9\u79cd\u7ec6\u8282\uff0c\u56e0\u4e3a\u4e0d\u80fd\u9760\u8fd9\u4e2a\u5199\u4ee3\u7801\uff0c\u4f1a\u51fa\u4eba\u547d\u7684\uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587171\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : FrankHB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @lomewcl \u4e0d\u7528\u67e5\u4e86\uff0c\u90fd\u6ca1\u7ed9 main\uff0c\u751a\u81f3\u90fd\u6ca1\u8bf4\u662f\u5b8c\u6574\u7684\u7ffb\u8bd1\u5355\u5143\uff0c\u90a3\u81f3\u5c11\u80fd\u7528\u91cd\u5b9a\u4e49::operator new \u7684\u65b9\u5f0f\u53ef\u79fb\u690d\u5730\u4fdd\u8bc1\u59cb\u7ec8\u4e0d\u6b63\u786e\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"587172\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : FrankHB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             ::operator new\u2192::operator new[]\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e c++\u6307\u9488\u6570\u7ec4\u957f\u5ea6\u7684\u95ee\u9898 \u8cc7&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/89669"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=89669"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/89669\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=89669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=89669"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=89669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}