Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to demonstrate where a c# variable is stored, stack or heap

Tags:

c#

I know c#, like Java, translating the source into bytecode and run by VM, for C# case is CIL stored in assembly and executed by CLR. Developers scarcely need to care about where the variable is on stack or on heap(handled by GC) as c++, right?

Is their any quick and straightforward way to demonstrate that a variable is stored on stack or on heap? For example, if I tell someone the reference type variables are stored on heap or the local value type variable on stack(right?). How can I show that explicitly? In C++, I can get a variable's memory address and view its value stored in stack or heap memory by VS memory window.

like image 969
Wason Avatar asked Jul 31 '14 03:07

Wason


People also ask

Where is AC commonly used?

Alternating currents applications AC is most commonly found in mains-wired buildings such as homes and offices. This is because generating and transporting an AC current across long distances is relatively easy.

How do you calculate alternating current?

In a simple circuit, the current is found by dividing the voltage by the resistance. An ac current is calculated using the peak current (determined by dividing the peak voltage by the resistance), the angular frequency, and the time.

How do you know if something is AC or DC?

Both AC and DC describe types of current flow in a circuit. In direct current (DC), the electric charge (current) only flows in one direction. Electric charge in alternating current (AC), on the other hand, changes direction periodically.

What is AC example?

AC is the form of current that are mostly used in different appliances. Some of the examples of alternating current include audio signal, radio signal, etc. An alternating current has a wide advantage over DC as AC is able to transmit power over large distances without great loss of energy.

What is direct current and alternating current?

Electric current flows in two ways as an alternating current (AC) or direct current (DC). In alternating current, the current keeps switching directions periodically – forward and backward. While in the direct current it flows in a single direction steadily.

How do you know if a place has AC?

A central air conditioner works with a compressor that is almost always located outside the home. This compressor is typically about three feet or four square, with grilles around it for venting hot air. You can also typically see a large fan inside the top cover of the compressor.


1 Answers

Developers scarcely need to care about where the variable is on stack or on heap, right?

Right. The technique chosen for the storage is an implementation detail of the runtime.

I note that you've omitted registers as a possible choice for storage. Lots of variables are enregistered, so they go on neither the stack nor the heap.

The better way to think of it is that there is short term storage and long term storage.

Is there any quick and straightforward way to demonstrate that a variable is stored on stack or on heap?

Attempt to compute the lifetime of the variable. If you can do so easily, and the lifetime is less than or equal to the duration of the method activation which created it, then it is likely on the short term pool. If not, it's likely on the long-term pool.

In C++, I can get a variable's memory address and view its value stored in stack or heap memory by VS memory window.

Doing so with the & operator can change where the variable is stored! If you take the address of a variable then it cannot be enregistered because registers don't have addresses. A technique which changes what it attempts to describe is an unreliable technique.

Also, you are perhaps forgetting that in C++ the stack and the heap are not the only possible storage locations.

For example, if I tell someone the reference type variables are stored on heap or the local value type variable on stack(right?). How can I show that explicitly?

Since those statements are false you cannot show them at all. It is simply false that references always go on the heap and that local variables of value type go on the stack. Example:

void M()
{
    string s = "";
    ...

The empty string is on the long term storage pool, but the empty string isn't stored in s in the first place. A reference to the empty string is stored in s. That reference can be put in the short term pool.

Remember, the actual referants of a reference type do not go in variables at all. Variables of reference type hold references -- that's why they're called reference types.

Now, a field of a reference type is a variable, and that variable does not have a known lifetime, so that variable must go on the long term storage pool:

class C { public int x; }
...
C M() { C c = new C(); return c; }

x is a variable of value type but because it is a field belonging to a reference type, it must be on the long-term pool. c is a variable of reference type so it holds a reference; the lifetime of the variable c is short, so c goes on the short-term pool. Again do not confuse the reference with the thing being referred to.

Example:

Action<int> M()
{
    int x = 123;
    return y => { x = y; Console.WriteLine(x); };
}

Local variable x is of value type but you cannot compute its lifetime and therefore it must be on the long term storage pool.

Since your statement that variables of reference type go on the heap and local variables of value type go on the stack is false, there is no way to demonstrate its truth.

like image 183
Eric Lippert Avatar answered Oct 13 '22 23:10

Eric Lippert