KiX Ortillan
KiX Ortillan

Reputation: 210

Does declaring a variable already occupy memory

Hi I have been searching for the answer to this quite long. I want to know things under the hood regarding when memory will be taken up by my code.

Ex. int myVar;

  1. Does this code already take up memory? by memory i mean the stack? and initially in .NET I have noticed that this line will have a default value of ZERO(0).
  2. But what does the variable really store? the value ZERO or an address to where the value ZERO(0) is stored?

Also from what I understand about reference type is that when i write this line of code

MyClass myObj; // declare only

myObj = new MyClass() //create instance

I want to know what happens under the hood. Are my assumptions correct? That myObj will reside at the stack and myObj is capable of storing address. and at my creating an instance the object is created at the heap and the address of that object is passed to the myObj variable.

The thing is, I want to know if what is more efficient(memory-wise and performance-wise) between the two below.

Code 1: int myVar; myVar = FunctionThatReturnsAnInteger(); Console.WriteLine(myVar);

Code

Console.WriteLine(FunctionThatReturnsAnInteger());

I want a good explanation for this and analogy is quite handy.

Thanks in advance. :)

Upvotes: 2

Views: 1846

Answers (1)

Slugart
Slugart

Reputation: 4680

Performance efficiency - more efficient but only by a few instructions, storing and loading the variable from the execution stack. Bear in mind that the Jitter may optimise your code at run-time, removing any unnecessary instructions.

Memory efficiency - more efficient as there is no local var defined on the execution stack.

Code clarity - having the definition of a variable closer to where it's used makes the code more usable. In my opinion having the one method returning into another is even cleaner however does have the downside that it's harder to inspect the value being returned (in VS 2013 this will be possible however).

IL with the temp variable:

  // Code size       13 (0xd)
  .maxstack  1
  .locals init ([0] int32 myVar)
  IL_0000:  call       int32 StackOverflowScratchPad.Program::FunctionThatReturnsAnInteger()
  IL_0005:  stloc.0
  IL_0006:  ldloc.0
  IL_0007:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_000c:  ret

and without:

  // Code size       11 (0xb)
  .maxstack  8
  IL_0000:  call       int32 StackOverflowScratchPad.Program::FunctionThatReturnsAnInteger()
  IL_0005:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_000a:  ret

Bonus - IL when assigning an value to temp var at the declaration:

  // Code size       15 (0xf)
  .maxstack  1
  .locals init ([0] int32 myVar)
  IL_0000:  ldc.i4.5
  IL_0001:  stloc.0
  IL_0002:  call       int32 StackOverflowScratchPad.Program::FunctionThatReturnsAnInteger()
  IL_0007:  stloc.0
  IL_0008:  ldloc.0
  IL_0009:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_000e:  ret

Upvotes: 4

Related Questions