. The nature of heap and stack (stack) in net

Source: Internet
Author: User

The computer's memory can be divided into code block memory, stack memory, and heap memory. code block memory is where the program machine code is stored when the program is loaded.

Stacks typically store local variables within a function.

Heap generally holds global variables, class object instances, and so on.

If only one object is declared, it is allocated an address space in the stack memory, and if it is instantiated again, it is allocated space in the heap memory.

1. Stack VS Heap

As the computer's memory allocation process is abstract, here is a simple program fragment to illustrate the effect of the harmony step on stack and heap memory:

The following Stackvsheap class has a person class and a Fun1 method, and when the Fun1 method is called,

When executing the first sentence, namely:

int i=3;

In. NET, in addition to string, object, class, delegate, interface, other types are numeric types. Generally (not all) is stored in stack memory.

here int i=3; is a non-static variable within a function, and a numeric type is a non-reference type, which allocates an area in stack memory to hold the variable's name and value.

When executing the second sentence, namely:

int j=i;

. NET also allocates a zone in stack memory to hold the name and value of the variable. And the address block is above the i=3 (LIFO).

Explain:

①fifo (full name: First In,first out): FIFO.

②lifo (full name: Last In,first out): LIFO.

When executing the third sentence, namely:

Person p = new person ();

, we can look at two steps:

1) Assign a P reference variable (pointer) to the type of person on the stack (pointing to the address on the heap);

2) Allocate a control on the heap to store instance data for the person class;

The specific process is as follows:

2. Value types and reference types

Understanding the above procedure, it is now easier to understand the value types and the variables of the reference type:

Let's look at the following diagram:

due to int j=i; is an int type and is a value type variable, then j=3 is a copy of i=3; therefore, modifying I will not modify J, and J will not modify I;

The person class in person p2=p is a reference type, so P2 and P point to the same heap address block, so modifying the value of P2 affects the value of P.

. The nature of heap and stack (stack) in net

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.