I have a block of code below with a single line commented out. What happens in the CreateArray method is the same thing that the commented out line does. My question is why does it work when the line b->ArrayItems = d is uncommented, but return garbage when commented out? I don't think I have to "fixed" anything, because all of the information is unmanaged. Is this assumption incorrect?
class Program { unsafe static void Main(string[] args) { someInstance* b = stackalloc someInstance[1]; someInstance* d = stackalloc someInstance[8]; b->CreateArray(); // b->ArrayItems = d; *(b->ArrayItems)++ = new someInstance() { IntConstant = 5 }; *(b->ArrayItems)++ = new someInstance() { IntConstant = 6 }; Console.WriteLine((b)->ArrayItems->IntConstant); Console.WriteLine(((b)->ArrayItems - 1)->IntConstant); Console.WriteLine(((b)->ArrayItems - 2)->IntConstant); Console.Read(); } } public unsafe struct someInstance { public someInstance* ArrayItems; public int IntConstant; public void CreateArray() { someInstance* d = stackalloc someInstance[8]; ArrayItems = d; } }