Just declaring a simple type variable allocating memory?

I am reading this question in C, and I have the same question in C #.

Is it correct to say that the code

int a;

Are four unassigned bytes reserved for abefore its initialization? Or initialization is where the bytes are allocated for the first time?

+4
source share
4 answers

An int declaration without assignment (as indicated in the comments) will be optimized by the compiler - it will not be displayed in the program at all.

, ( ) int, (.. int , , ).

, int ( ), , ( , ).

int.

:

int . Int (4 ), , , , 4 . , 4 ( ) , 4 , ( ). . , .

, .NET 2 - #, # IL JIT, IL . , "", , , .

, ...

void Foo() {
  {
    int a = 5;
    Console.WriteLine(a);
  }
  {
    int a = 7;
    Console.WriteLine(a);
  }
}

... , , a , . .

, - , (, ), .

, , # JIT - , , . .

+4

An int # 4 ( , ).

,

int a;

int a = 0;

int 0.

+2

, .
int ValueType, 4 . # : . , int, , , , , , .

Class MyType
{
...
}

MyType s; // Just a pointer
s=new MyType(); //Now allocating happens.

. MSDN.
. MSDN.

MSDN:

# : . (), . , ; , . , , ( ref out , . ref ( #) - (# Reference)).

+1

A simple variable declaration, as in your example, will contain a link until you create an object with a new keyword.

-3
source

All Articles