3
//Consider this declaration
string name;

Here string variable name is an unassigned variable,does this declaration reserve any memory for name if it does not initialised?

Shamseer K
  • 4,964
  • 2
  • 25
  • 43
  • 7
    If it's not used anywhere it will probably be stripped out by the compiler – Vsevolod Goloviznin Feb 05 '15 at 14:42
  • that's generally what declaring a variable does, but if it's never used the compiler will probably optimize that away. – DLeh Feb 05 '15 at 14:42
  • 1
    Is it a class field or a local variable? The answer is different depending on the place you are using the code. – Steve Mitcham Feb 05 '15 at 14:43
  • Who cares? If you're genuinely not using it, kill it. – Martin Milan Feb 05 '15 at 14:48
  • 2
    Using ILSpy, I can confirm that `string name;` and `string name = "Test";` are optimised away from DEBUG and RELEASE builds if they are not used elsewhere in a method. – Jason Evans Feb 05 '15 at 14:48
  • I recommend reading Jon Skeets answer here [how-much-space-do-string-empty-and-null-take] (http://stackoverflow.com/questions/6601446/how-much-space-do-string-empty-and-null-take) as it will explain a lot. TL;DR a `string` variable is a reference, thus it will take up the size of a reference (e.g. 4 bytes on 32bit CPU) even if it's NULL. – Jason Evans Feb 05 '15 at 14:50
  • @JasonEvans -if the variable is a member of class situation is different right? – Shamseer K Feb 05 '15 at 15:10
  • Yes, if it's a `field` in a class, then it will never be removed, even if it's NULL or not used elsewhere. – Jason Evans Feb 05 '15 at 15:36
  • 1
    @JasonEvans Not necessarily. If it's `private` and it's never read from within the class, the compiler can in fact determine that it's unused. Or if the field is `internal` and never accessed anywhere in the assembly. If it's public then it'd need to be there. – Servy Feb 05 '15 at 16:12

2 Answers2

4

It is not unassigned. All classes/structs receive their default value. For a string it is null.

If it is a local variable, then optimisation will tend to remove it. If its an instance variable then memory will be allocated (I think, C# spec is unclear).

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • why you are saying "it is not unassigned".if it is a local variable and if i try do any operation on name,Compiler shows "use of unassigned variable". – Shamseer K Feb 05 '15 at 15:07
  • @ShamseerKSmr And it's quite right in its error; you cannot use an unassigned variable. You need to assign a value to it first. – Servy Feb 05 '15 at 15:20
-1

A variable local to a method doesn't reserve any memory, registers are allocated to it depending on how it is used and how other variables are used. As long as it's not used, no register is allocated to it.

You can have a large number of variables inside a method but there is a limited number of register in your CPU so the compiler optimize your code to re-use registers. For more information, see Register allocation.

No, string name; doesn't reserve any memory.

Guillaume
  • 12,824
  • 3
  • 40
  • 48
  • Local variables *may* be enregistered, and they may not be. To say that locals never consume memory is very much false. In fact, a pretty significant percentage of local variables in most programs end up spending at least *some* time in memory. – Servy Feb 05 '15 at 15:23
  • They don't *reserve* any memory (implying when they are not used). – Guillaume Feb 05 '15 at 15:44
  • Stack space will be allocated for the local variable, which is going to end up reserving memory. If the compiler optimizes it out because it's unused, then there will be no existence of the variable at all at runtime (not even a register). – Servy Feb 05 '15 at 15:46