(Conversion) c # object memory model,
Object Memory Model
C # Object Memory Model
The main purpose of writing this blog is to deepen your understanding. If something is wrong, please forgive me.
C # Object Memory Model:
I. stack memory and heap memory
1. stack memory
Automatically assigned and released by the compiler. It is mainly used to save some local variables and function parameters, for example, int a = 10, then the compiler will automatically open up a piece of content on the stack to store variable.
2. heap memory
The application is manually applied and released by the programmer. In C ++, the compiler will not be released after the new keyword is applied, and must be released through delete. For C #, The new Keyword is applied, because of the garbage collection mechanism of the compiler, programmers do not need to manually release the memory. For example, we declare an object zhangsan for student, and student zhangsan = new student (). First, the compiler allocates the memory storage variable zhangsan on a stack, then a block of memory is opened on the stack to store the student object, and finally the address on the stack is stored in the variable zhangsan. If we create another object lisi, student lisi = new student (), then, lisi = zhangsan actually changes the lisi value stored on the stack, that is, the address of the object pointed to on the stack, as shown in:
3. Comparison of stack memory and heap memory
Memory Allocation
STACK: the Back-to-first-out formula. The Compiler automatically allocates the size of the corresponding type. The size of the allocation is limited by the size of the stack;
Heap: randomly allocated. The size is manually applied by the programmer. The allocated size is limited by the virtual memory.
Efficiency
STACK: high
STACK: relatively low
Ii. Value Type and reference type
1. Value Type
Value-type variables store the value of variables directly in the stack memory;
2. Reference Type
The reference type variable stores the address of the memory where the variable is located. The actual data of the reference type variable is stored in the heap memory, and the variable itself is stored in the stack memory, it stores the address pointing to the heap, usually four bytes, and stores an address value.
Value types in C #: struct and enum (for int and float types, all belong to the struct type)
Reference Type: class, delegate, array, interface
As shown in:
Iii. Deep copy and light copy
We often encounter this problem in programming. We already have an object a, and object a has some specific values. Now we want to create a copy of object a, that is, object B, we hope that object B will not change the value of object a at the same time, that is, object a and object B are two completely independent objects, that is, deep copy. The concept of deep copy: the source object and the copy object are independent of each other. Any changes to one object will not affect the other object.
When two objects direct to the same address, if we change the value of one object, the other object is also changed accordingly, which is a shortest copy. The concept of shallow copy: When shallow copy is used, the two objects are not completely "separated". Changing the content of the top-level object does not affect the content of the other object, but changes the content of the sub-object, the two objects are modified at the same time.
The difference is determined by the replication memory or the replication pointer when the sub-object is copied. Deep copy re-allocates a memory space for the sub-object and copies the content. The light copy only points the pointer to the original sub-object.
Something worth attention
(1): A String object is a reference object, but it is very special. It is represented like a value object, that is, it is assigned a value, divided, and merged, instead of operating on the original string, A New String object is returned.
(2): The Array object is a reference object. When assigning values, another reference of the source object is actually returned; therefore, if you want to copy an array object (deep copy), you need to create a new set of objects, and then copy the values of the source array to the target object one by one.