First, before we talk about stacks, let's look at value types and reference types:
1, let's look at how value types and reference types are stored:
Reference type: The reference type is stored in the heap. When a type is instantiated, an instance of the spatial storage class is opened up in the heap. The reference to the class object is stored in the stack.
Value type: A value type is always allocated where it is declared, when it is stored on the stack when it is a local variable, and when the class object's field is stored in the heap.
What is heap what is the stack we explain behind.
Figure 1-1
2, let's look at the difference between a reference type and a value type:
Both the ① reference type and the value type inherit from the Systerm.object class. In contrast, almost all reference types are inherited directly from systerm.object, whereas value types are subclass Systerm.valuetype classes that inherit Systerm.object.
② we assign a value to a variable of a reference type, we are only assigning a reference to the object, whereas assigning a value type variable creates a copy (the copy does not understand?). To say the popular point, is to clone a variable).
Text is not enough image? Let's take a look at the code
Figure 1-2
3, let's look at the memory allocation of reference types and value types (we look at the code and the graph)
Figure 1-3
Figure 1-4
After looking at figure 1-3 and figure 1-4, you may have asked me how you know the address of the variable in the stack. Hey, wait, I'll teach you to play the pointer in C #
From the above two graphs we can see:
The structure of the ① stack is LIFO, which means that the life cycle of the variable J ends before the variable s, and the life cycle of the variable s ends before the variable i,
② stack address from high to bottom allocation
References to ③ types are also stored in the stack
Second, for the detailed introduction of heaps and stacks, we look down.
1, some people always do not understand the heap and stack of the term. Let me explain:
Heap: In C is called Heap, in C # is actually called the managed heap. Why call the managed heap, we look down.
Stack: is the stack, because with the heap called awkward, just short stack.
2, managed heap:
The managed heap is different from the heap, which is managed by the CLR (the Common Language runtime (Common Language Runtime)) and automatically cleans up garbage in the heap when the heap is full. So, for. NET development, we don't need to be concerned with memory release issues.
3, someone is always confused about the memory stack and the data structure stack, let's see what is the memory stack, what is the data structure stack
① data Structure Stack: is a last-in, first-out, it is a concept, as can be seen in Figure 4-1, the stack is a LIFO data structure.
② Memory Stack: There are two stores in memory (heap area, stack area).
Stack: stored function parameters, local variables, return data equivalent, automatically released by the compiler
Heap area: An object that holds a reference type, released by the CLR
C # heap and Stack