views:

149

answers:

7
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.

+1  A: 

No it means t1 has not been assigned yet. Once you call the new Test() t1 will be allocated memory to store the size of a Test object and is assigned a memory address.

Yes memory is only allocated at run-time.

James
+5  A: 

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.

Neil Barnwell
+2  A: 

The reference t1 is assigned on the stack frame of the Main method - the value you assign to it (i.e. the 'new Test()' part) is allocated on the heap at runtime - this is why the t1 variable exists before that line has executed and is null.

Lee
MSDN says using new creates object on heap. link for reference http://msdn.microsoft.com/en-us/library/51y09td4(VS.71).aspx
Kavitesh Singh
Yes the object is created on the heap, but the reference will be on the stack
Lee
A: 

run the application in the debugging mode, F5 is mostly the keyboard shortcut in visual studio. Put a breakpoint at this statement. Use the F10 to step to next statement. Moment the debugger steps next statement you would see the object being created.

Kavitesh Singh
+2  A: 
Test t1;

This part allocates enough space only for a reference (probably 4 bytes in many cases, but I think it really depends on the .NET framework implementation), and is determined at compile time and allocated (I think) when Main is called.

t1 = new Test();

This line, when it runs, will allocate enough space to store the data for a Test object and then assign the existing reference variable t1 to refer to that newly allocated memory. So now you have the space for t1 plus the space for new Test() both allocated.

Edit: To respond to your edit, static member variables are different, and your local variable behaves differently than they do. However, I don't believe that static member variables are allocated at compile time. I think they are allocated when the type containing them is loaded (which, seems to be when any function is called that refers to the type).

class Test2
{
   public static Test f1 = new Test();
}

This code would create a new instance of Test as soon as a function that contains any a reference to type Test2 is called.

BlueMonkMN
Thanks for mentioning, about static variables. :)
vaibhav
+2  A: 

At the start of 'Main', the heap memory for the instance hasn't been allocated yet (and won't be until new Test()). It will remain allocated until it's garbage-collected.

The local variable, Test t1 (stack-based), that will reference the heap memory does exist (and exists for the whole time that 'Main' is executing).

Both of these are run-time assignments, but differ in terms of where the memory resides and for how long.

Static fields are allocated when the type loads (and are initialized some time between then and the type's first use).

By 'compile-time', I'm not sure if you're referring to JIT compile - but whichever way, the memory is allocated separately from compilation.

Dave Cluderay
+1  A: 

You should be able to figure this one out yourself. Just think about it:

  • When you allocate memory, you "use" memory for something.
  • A program that does not run, does not use memory.
  • A program that does run, uses memory.
  • At run-time, means a program is running.
  • Memory is used, allocated, at run-time.

Allocating memory compile-time, just doesn't make any sense...

Svish
Thanks, it was a silly question.
vaibhav
No worries! (By the way, even though it was silly question, you should accept an answer. Or if it was *really* silly, maybe just delete the question, but yeah... just saying :)
Svish