C # Difference Between Stack and stack,

Source: Internet
Author: User

C # Difference Between Stack and stack,

 

 

 

Thread Stack: Stack managed Heap

When developing programs using the. Net Framework, we don't need to worry about the memory allocation problem, because the GC manager gives us everything. If we write the following two pieces of code: Code Segment 1:

Public int AddFive (int pValue) {int result; result = pValue + 5; return result ;}

Code Segment 2:

Public class MyInt {public int MyValue ;}

Public MyInt AddFive (int pValue) {MyInt result = new MyInt (); result. MyValue = pValue + 5; return result ;}

Question 1: Do you know how pValue and result are stored in the memory and the lifecycle when code segment 1 is executed? What about code segment 2? To solve the above problems, we should have a clear understanding of the Stack and the managed Heap (Heap) under. Net. If you want to improve program performance, understand the stack and heap, you must! In this article, we will start from the stack, stack, and type variables to explain the program we have written. C # When the program runs on the CLR, the memory is logically divided into two major blocks: Stack and heap. These two basic elements form the running environment of our C # program.

I. Stack vs Stack: What is the difference?

The stack usually stores the steps for executing our code, such as the AddFive () method, int pValue variable, int result variable, and so on in code segment 1. Objects and data are mostly stored on the stack. (Translator's note: Ignore Compiler Optimization) we can think of the stack as a box stacked together. When we use it, each time we take a box from the top. The same is true for the stack. When a method (or type) is called, it is removed from the top of the stack (called a Frame, annotation: Call Frame), followed by the next one. The heap is not a repository. It stores the various objects we use and other information. Different from the stack, they are not cleared immediately after being called.

1. Stack and stack

 

 

 

Stack memory does not need to be managed or managed by GC. When the top element of the stack is used up, it is immediately released. GC (Garbage collection: Garbage Collector) is required for the heap.

2. What elements are allocated to the stack? What is allocated to the heap?

During program execution, there are four main types in the stack and heap: Value Type, reference type, pointer, and instruction.

Value Type: in C #, It is inherited from System. the ValueType type is called the value type. It mainly includes the following types (the supported types in CLR2.0 are increased ): * bool * byte * char * decimal * double * enum * float * int * long * sbyte * short * struct * uint * ulong * ushort

Reference Type: The following is a reference type inherited from System. Object: * class * interface * delegate * object * string

Pointer: In the memory area, a reference to a type is usually called a "Pointer", which is managed by the CLR (Common Language Runtime: Runtime in public Language, we cannot display and use it. It should be noted that a type of reference, that is, pointer and reference type, are two completely different concepts. A pointer occupies a memory area in the memory. It only represents one memory address (or null), and the other memory area it points to is our real data or type.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.