Is Int32^ i = gcnew Int32() allocated on managed heap?-Collection of common programming errors
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 j = 0;
while (GC::GetGeneration(o) == 0) {
Int32^ i = gcnew Int32();
j += 4;
if (j % 100 == 0) {
printf("%d\n", i);
}
}
printf("Generation 0 collection happens at %ld\n", j);
return 0;
}
It runs with output
14849324
14849260
14849196
14849132
14849068
14849004
14848940
14848876
14848812
14848748
14848684
14848620
14848556
14848492
14848428
14848364
Generation 0 collection happens at 146880