{"id":90819,"date":"2020-05-11T06:37:58","date_gmt":"2020-05-10T22:37:58","guid":{"rendered":"http:\/\/4563.org\/?p=90819"},"modified":"2020-05-11T06:37:58","modified_gmt":"2020-05-10T22:37:58","slug":"c-%e4%b9%8b-new-%e5%87%bd%e6%95%b0%e7%96%91%e9%97%ae%ef%bc%8c%e4%bd%a0%e4%bb%ac%e4%b9%9f%e6%9c%89%e8%bf%99%e7%a7%8d%e6%83%85%e5%86%b5%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=90819","title":{"rendered":"C++ \u4e4b new \u51fd\u6570\u7591\u95ee\uff0c\u4f60\u4eec\u4e5f\u6709\u8fd9\u79cd\u60c5\u51b5\u5417\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  C++ \u4e4b new \u51fd\u6570\u7591\u95ee\uff0c\u4f60\u4eec\u4e5f\u6709\u8fd9\u79cd\u60c5\u51b5\u5417\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sl0000 <\/span>  <span><i><\/i> 17<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u8bfb\u53d6 new \u51fa\u6765\u7684\u5bf9\u8c61\u5360\u7528\u7684\u8fde\u7eed\u5806\u5185\u5b58\uff0c\u5076\u7136\u53d1\u73b0\u65f6\u6709\u65f6\u65e0\u7684\u5b58\u5728\u4e00\u4e9b\u810f\u6570\u636e\uff0c\u4f46\u662f\u901a\u8fc7\u6808\u5185\u5b58\u5206\u914d\u7684\u5bf9\u8c61\u4e0d\u4f1a\u5b58\u5728\u810f\u6570\u636e\u3002\u810f\u6570\u636e\u7684\u51fa\u73b0\u4e0e\u5185\u5b58\u5bf9\u9f50\u4f18\u5316\u6709\u5173\u3002<\/p>\n<p>new \u51fd\u6570\u5e76\u4e0d\u4f1a\u628a sizeof(Cls)\u7684\u8fde\u7eed\u5185\u5b58\u5168\u90e8\u6e05\u7a7a\uff0c\u53ea\u4f1a\u6e05\u9664\u4f7f\u7528\u5230\u7684\u5185\u5b58\uff0c\u5185\u5b58\u5bf9\u9f50\u7684\u7a7a\u7f6e\u7684\u5185\u5b58\u5e76\u4e0d\u4f1a\u88ab\u6e05\u96f6\u3002<\/p>\n<pre><code>class Cls { Public;     long a;     char b;     long c;     char d; };  char s[256] = {}; for (int i = 0; i &lt; 256; i++) {     s[i] = 0xff; }  Cls * pa = new  (s) Cls {1,2,3,4}; <\/code><\/pre>\n<p>(lldb) memory read &#8211;size 4 -format x &#8211;count 64 pa<\/p>\n<p>\u7528\u7684 Xcode\uff0c\u8fd9\u79cd\u60c5\u51b5\u662f\u6240\u6709\u5e73\u53f0\u90fd\u4f1a\u51fa\u73b0\u5417\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u8fd9\u79cd\u8bbe\u8ba1\u662f\u4e3a\u4ec0\u4e48\uff1f<\/p>\n<pre><code>Cls * pa = new  (s) Cls (); <\/code><\/pre>\n<p>\u7ecf\u63d0\u9192\uff0c\u6211\u6539\u4e3a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u786e\u5b9e\u662f\u90fd\u88ab\u6e05\u96f6\u4e86\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1313014\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : felix021 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f30\u8ba1 C++\u89c4\u8303\u5bf9 placement new \u6ca1\u6709\u505a\u5185\u5b58\u6e05\u96f6\u7684\u8981\u6c42\uff0c\u5177\u4f53\u505a\u4e0d\u505a\u5c31\u770b\u7f16\u8bd1\u5668\u7684\u5b9e\u73b0\u4e86\u3002<\/p>\n<p>ref\uff1a https:\/\/stackoverflow.com\/questions\/10490191\/does-placement-new-zero-out-the-memory                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1313015\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : felix021 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ce8\uff1a\u4e0d\u505a\u67d0\u4ef6\u4e8b\u60c5\uff0c\u5f53\u7136\u662f\u4e3a\u4e86\u66f4\u5feb                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1313016\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dearmymy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u8ddf\u7f16\u8bd1\u5668\u6709\u5173\u5427\uff0c\u800c\u4e14\u4f60 debug release \u4f30\u8ba1\u4e5f\u4e0d\u4e00\u6837\uff0c\u4e5f\u8bb8 debug \u5e2e\u4f60\u6e05\u4e86\u3002release \u5c31\u4e0d\u7ba1\u4f60\u3002<br \/>\u4e0d\u77e5\u9053\u4f60\u60f3\u5e72\u5565\uff0c\u4f60\u8981\u662f\u5acc\u5f03 new \u51fa\u6765\u7684\u5185\u5b58\u5bf9\u9f50\u7a7a\u4f59\u90e8\u5206\uff0c\u53ef\u4ee5\u628a\u5185\u5b58\u5bf9\u9f50\u8bbe\u7f6e\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1313017\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : sl0000 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @dearmymy \u4e3b\u8981\u662f\u56e0\u4e3a\u9605\u8bfb\u7684\u65f6\u5019\u4f1a\u88ab\u5e72\u6270\u5230\uff0c\u4e00\u4e9b\u6ca1\u6709\u7528\u7684\u6570\u636e\u7a7f\u63d2\u5176\u4e2d                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1313018\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dearmymy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @sl0000 \u90a3\u6784\u9020\u51fd\u6570\u81ea\u5df1\u6e05\u96f6\u4e0b\uff0c\u5728\u52a0\u4e0a#pragma pack(1) \u5c31\u4e0d\u4f1a\u51fa\u73b0\u5b57\u8282\u5bf9\u9f50\u95f4\u9699\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1313019\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hu8245 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             operator new                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1313020\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Wirbelwind <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             operator new \u548c delete \u672c\u8eab\u4e0d\u4f1a\u6e05\u9664\u5185\u5b58\u6570\u636e\uff0c\u56e0\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u5e95\u5c42\u53ef\u80fd\u4f1a\u6e05\u9664\u3002<\/p>\n<p>\u6211\u8bb0\u5f97 linux \u5206\u914d\u65b0\u6808\u65f6\u5019\u662f\u4f1a\u6e05 0 \u7684\u3002<\/p>\n<p>(\u67d0\u4e2a\u534f\u7a0b\u5e93\u662f\u5206\u914d\u6808\u4e5f\u4f1a\u6e05 0\uff0c\u4f46\u662f\u91cd\u590d\u4f7f\u7528\u67d0\u4e2a\u6808\u7684\u65f6\u5019\u4e0d\u4f1a\u6e05 0.)                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1313021\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : felix021 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @dearmymy pragma pack(1) \u8fd8\u662f\u8981\u614e\u7528\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>C++ \u4e4b new \u51fd\u6570\u7591\u95ee\uff0c\u4f60\u4eec&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\/90819"}],"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=90819"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/90819\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90819"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}