Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Allocation Question?

public static void Main()
        {
            Test t1 = new Test();
}

when will t1 (reference variable) will get memory, at compile time or run time.
I think it should be run time. But when I put a breakpoint at Main Method and Put a Watch for t1, it was null. So it means t1 was in memory.

Please correct me If I am wrong.

Edit: I heard Static Member Variables are assigned at compile time.

like image 604
Vaibhav Jain Avatar asked Jan 21 '10 11:01

Vaibhav Jain


1 Answers

Memory is only allocated at runtime (at compile time your app isn't running).

At runtime, your t1 variable will only have a value (i.e. not null) after the assignment has occurred, so it depends where you put your breakpoint. If you put your breakpoint on the closing brace of your Main method and check the watch window when it's hit, you'll see what I mean.

I.e. If you put your breakpoint on the assignment line Test1 t1 = new Test1(); then that line hasn't been executed yet, so the assignment hasn't happened.

like image 95
Neil Barnwell Avatar answered Sep 22 '22 12:09

Neil Barnwell