What happens when you assign a link to a public variable inside a method on a public static variable

Meanwhile, after reading on the Internet, I found out that static variables always have the same memory address. Therefore, when compiling the program, the compiler will decide which memory address should be assigned to static variables. The reading that made me think about what happens when you do:

class Foo {} class Program { public static Foo someStaticVar; static void Main(string[] args) { Foo localVariable = new Foo(); int x = 4; someStaticVar = localVariable; // is someStaticVariable still will have the same address? } // variable x will be pushed of the stack // localVariable will be pushed of the stack // what happens then with someStatic var? } 

I also learned that when declaring variables inside a method, they will be pushed onto the stack when created and popped from the stack when the method returns. If all this is true, then someStaticVar should disappear, but it is not.

I am sure that I must understand something wrong. Or maybe on the line someStaticVar = localVariable; a deep copy of this object is running, but I doubt it because there are many questions on the Internet about how to make a deep copy of the object, and they are very different from this approach.

+4
source share
4 answers

In the above example, localVariable is only a local variable for the Main method. It falls out of scope after the completion of the main method. But since you assigned it to a static field, the instantiated Foo will continue to live outside the Main method. And since this is a static field, it will live even outside the class of the program.

So what happens step by step:

 static void Main(string[] args) { // an instance of Foo is created and pushed on the heap // localVariable is now pointing to the address of this instance // localVariable itself is stored on the stack Foo localVariable = new Foo(); // someStaticVar is now pointing to the same location on the heap as // the localVariable - the Foo instance created earlier someStaticVar = localVariable; } // at this stage the Main method finishes executing and all local // variables are falling out of scope. But since you have a static variable // pointing to the Foo instance that was created inside the Main method this // instance is not illegible for garbage collection because there are still // references to it (someStaticVar). 

If someStaticVar was an instance field, and not static, then the same process will occur, except that the instance will fall out of scope if there are no more references to the containing class (Program).

+4
source

when declaring variables inside a method, they will be pushed onto the stack when created and popped from the stack when the method returns.

Foo localVariable = new Foo ();

will create a Foo object on the heap, and the link will be saved on the stack. when the method completes, the links are removed from the stack. The garbage collector will do the work of removing Foo from the heap, since no reference to the Foo object will be provided. But,

someStaticVar = localVariable;

calls, someStaticVar refers to the Foo object on the heap. Even after the method exit, someStaticVar will still refer to the Foo object. therefore, the garbage collector does not collect this Foo object. The main thing to remember when the Reference type object is created, the object is created on the heap, and the link is stored on the stack.

The questions are "where is the static field stored?", The fields of the object instance are stored on the heap, the local variables are stored in stacks, but where is the "static variable exists in memory? "

+2
source

There is no deep copy of the object: the object itself is stored on the heap, not on the stack. You are correct that someStaticVariable will always have the same address. Your misunderstanding, I believe, is what is stored at this address.

When you declare a variable of a reference type (any type of object , such as Foo in your code), the variable itself is not an object: it is a variable that stores the address of your object. This address is just a number. So, "the address of someStaticVariable " is not the address of Foo itself; This is the address of the Foo address.

In the same way, therefore your someStaticVar does not “disappear”. What happens in your code is that Foo is created at some address in memory, and localVariable by a value representing the address in Foo memory. When you execute someStaticVar = localVariable , what happens is that the address is copied from localVariable to someStaticVar , and therefore both variables point to the same Foo . When localVariable disappears, someStaticVar no effect.

+1
source

In this case, Foo is a class, so it is a reference type. A variable of type Foo is actually just a pointer to the location in memory where the Foo object is actually stored.

Saying that a static variable has a constant memory address, it does not state that the value of this memory address is constant. The address of the pointer to the Foo object will not change, but the number (in this case, the address) that is stored in this place in the fixed memory can easily vary between different potential Foo objects (or null ).

In addition, since C # is a managed language, and not a language without changes, it is not even worth saying that a static variable will have a constant memory address. This is certainly possible, but it really is a C # memory manager implementation detail that you should not rely on.

+1
source

Source: https://habr.com/ru/post/1434858/


All Articles