When is memory allocated in the .NET compact framework? If there is a difference between value and reference types, please detail. Documentation or steps to confirm are appreciated.
In particular, consider this scenario…
private MyClass item; // here? public void MyMethod() { item = new MyClass(); // or here? }
There are 3 different ways memory is allocated.
Static:
These are bound and allocated at compile time. Global static variables for example.
Stack Dynamic:
These are bound during runtime and pushed onto the stack. Such as a local variable in a function call.
Heap Dynamic:
Now heap dynamic also has a few different ‘sub categories’ such as implicit and explicit, but I won’t go into that detail.
When you declare
a reference to MyClass is pushed onto the stack. It is only a reference and nothing more. Its value is null at that point.
It is at that point where memory is explicitly allocated on the heap by calling ‘new MyClass()’ and item then references it.
So in actuality, you have 2 variables after you call MyMethod. A refernce type named item, and an unnamed variable on the heap which item references that is of type MyClass.