3

What is the value in memory of, for example, integer value (int) after declaration but not initialization? In "CLR vi C#" Richter writes, that value types is initialized with 0, but not allowed to be used. So what will be in memory after declaration of variable like this

int testVar;

And how is mechanism of initializing check implemented?

Arсiom Prudnikaŭ
  • 313
  • 1
  • 5
  • 20

3 Answers3

5

Types are initialized with memory that is all zeros. I don't know if this is according to the specification for all value types so you can't count on this unless you check. For different value types zeros in memory can mean different things depending on what the type represents.

Value types are auto initialized and can be used when they are a field of a class and not local variables. As far as I know there is no initialization check in the CLR itself. The initialization check is performed by the compiler and it reports compile time error when uninitialized variable is used.

Stilgar
  • 22,354
  • 14
  • 64
  • 101
  • +1 this check is a *C# compiler* feature, there is no such check in the CLR – BrokenGlass Mar 05 '12 at 14:08
  • So, if i understand you correctly, i can write some IL code that declares, for example, integer variable and use it as zero-initialized variable? – Arсiom Prudnikaŭ Mar 05 '12 at 14:10
  • Yes, I believe so. The declaration of a variable is not an execution step. It is in a way baked in the source code. On the other hand the initialization is execution step. This is why you can't put a breakpoint on a row with declaration but not initialization. – Stilgar Mar 05 '12 at 14:13
  • 1
    I just checked. The declaration of locals is a single step (at least in IL) for all the locals before the body of the method. – Stilgar Mar 05 '12 at 14:18
0

As far as I know, the declaration reserves some memory according to how may bytes this specific type needs. These bytes could in theory be randomly filled with whatever was physically occupying those specific bytes of hardware.

Lindan
  • 110
  • 9
  • Yes, declaration reserves some memory, but the question is what does it exactly store there before initialization. If we us `int` as a class field it is initialized with 0 by default. – Arсiom Prudnikaŭ Mar 05 '12 at 14:03
  • 1
    No they can't be filled randomly. Not in .NET. – Stilgar Mar 05 '12 at 14:04
  • So things have changed a bit since I learned about variable declaration 20 years ago... ;-) – Lindan Mar 05 '12 at 14:20
  • 1
    In C/C++ they still work the same way. Java and .NET aim to be deterministic in as many ways as possible so according to the specifications memory is zeroed out and operators never have unspecified or undefined behaviour. This sacrifises speed in certain situations for robustness. – Stilgar Mar 05 '12 at 17:46
0

In c#, some types allow you to have variables that can have a null value, like Nullable variables (Int32? intAux). This type will be null, at first. Otherwise, Int32 variables do not allow that you have null values.

And you can verify if a variable was initialized or wasn't, doing something like (intAux == null).

Lukinha RS
  • 410
  • 4
  • 12
  • Thanks, but I know this. My question is what is stored IN MEMORY after declaring a variable, but before assigning some value to it? – Arсiom Prudnikaŭ Mar 05 '12 at 14:07
  • 2
    Nullable types are implemented as a struct with a bool field HasValue and the normal field. Still these type are initialized with all zeros. As it happes when bool is 0 it is actually false so HasValue is false and this is how the variable becomes null which is different from reference type nulls. – Stilgar Mar 05 '12 at 14:20