{"id":4080,"date":"2014-03-30T07:28:07","date_gmt":"2014-03-30T07:28:07","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/is-int32-i-gcnew-int32-allocated-on-managed-heap-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:28:07","modified_gmt":"2014-03-30T07:28:07","slug":"is-int32-i-gcnew-int32-allocated-on-managed-heap-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/is-int32-i-gcnew-int32-allocated-on-managed-heap-collection-of-common-programming-errors\/","title":{"rendered":"Is Int32^ i = gcnew Int32() allocated on managed heap?-Collection of common programming errors"},"content":{"rendered":"<p>I have got the answer. gcnew will allocate the object on managed heap, even the type is a value type.<\/p>\n<p>Therefore, Int32^ i = gcnew Int32() will allocate the newly created object on managed heap.<\/p>\n<p>The following code can prove this:<\/p>\n<pre><code>#include \n#using \n\nusing namespace System;\n\nint main(void) {\n    Object^ o = gcnew Object();\n    long j = 0;\n\n    while (GC::GetGeneration(o) == 0) {\n        Int32^ i = gcnew Int32();\n        j += 4;\n\n        if (j % 100 == 0) {\n            printf(\"%d\\n\", i);\n        }\n    }\n\n    printf(\"Generation 0 collection happens at %ld\\n\", j);        \n\n    return 0;\n}\n<\/code><\/pre>\n<p>It runs with output<\/p>\n<pre><code>14849324\n14849260\n14849196\n14849132\n14849068\n14849004\n14848940\n14848876\n14848812\n14848748\n14848684\n14848620\n14848556\n14848492\n14848428\n14848364\nGeneration 0 collection happens at 146880\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have got the answer. gcnew will allocate the object on managed heap, even the type is a value type. Therefore, Int32^ i = gcnew Int32() will allocate the newly created object on managed heap. The following code can prove this: #include #using using namespace System; int main(void) { Object^ o = gcnew Object(); long [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4080","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4080"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4080\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}