Comparison Between Stack and stack in. Net #2

Source: Internet
Author: User

Source:
Http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory2B01142006125918PM/csharp_memory2B.aspx

Although we do not need to worry about memory management and garbage collection in. NET Framework, we should understand them to optimize our applications.Program. At the same time, we also need to have some basic knowledge of memory management mechanisms, which can help to explain the behavior of variables in our daily program writing. In this article, I will explain the behavior of passing parameters through methods that we must pay attention.

In the first part, I introduced the basic functions of stacks and stacks, how to allocate the value type and reference type during program execution, and also talked about pointers.

* Parameters, big issues

Here isCodeDetailed introduction during execution, we will go deep into the method calling process in the first part...

When we call a method, the following occurs:

1. during method execution, first allocate space for methods in the object instance on the stack, and then copy the methods to the stack (the stack is called a frame at this time ), however, this space only stores instructions for executing methods, and there are no data items in the methods.
2. The call address (or pointer) of the method is placed on the stack. Generally, it is a goto command, which allows us to know where to go back and continue executing the program after the method is executed. (It is better to understand this, but it is not necessary because it does not affect our encoding)
3. Space is required for allocating and copying method parameters, which requires further attention.
4. The control is passed to the frame at this time, and then the thread starts to execute our code. Therefore, another method is called "call stack ".

The sample code is as follows:

Public int addfive (INT pvalue)
{
Int result;
Result = pvalue + 5;
Return result;
}

In this case, the stack is opened like this:

As discussed in the first part, we need to check whether the parameter on the stack is a value type or a reference type. The value of the value type will be copied to the stack, and the reference (or pointer) of the reference type will be copied to the stack.

* Value type transfer

First, when we pass a value type parameter, a new space is allocated on the stack, and the value of this parameter is copied to this space.

Let's look at the following method:

Class class1
{

Public void go ()
{

Int x = 5;

Addfive (X );

Console. writeline (X. tostring ());
}

Public int addfive (INT pvalue)
{

Pvalue + = 5;

Return pvalue;
}
}

Method go () is placed on the stack, and then executed. The value "5" of the integer variable "X" is placed in the top space of the stack.

The addfive () method is then placed on the top of the stack, and the parameter value of the method is copied to the top of the stack, and the value of this parameter is a copy of "X.

After the addfive () method is executed, the thread returns the address of the Go () method through the pre-placed command, and then sorts the variable pvalue and method addfive () from the top of the stack () remove:

So the output value of our code is "5", right? The key here is that all input method value type parameters are copied and copied, so the values in the original variables are retained without being changed.

It must be noted that if we want to import a very large value type data (such as a large amount of data struct type) into the stack, it will occupy a very large memory space, in addition, it takes too many processor cycles to copy and copy data. The stack does not have infinite space. It is like a cup filled with water under a tap, which may overflow at any time. Struct is a value type member that can store a large amount of data and must be used with caution.

Here is a struct with the Data Type of memory amplification:

Public struct mystruct

{

Long A, B, C, D, E, F, G, H, I, J, K, L, M;

}

Let's see what happens when we execute the go () and dosometing () methods:

Public void go ()

{

Mystruct x = new mystruct ();

Dosomething (X );

}

Public void dosomething (mystruct pvalue)

{

// Do something here ....

}

This will be very inefficient. Imagine that if we pass mystruct for 2000 times, you will understand how the program crashed.

So how should we solve this problem? You can pass reference of the original value in the following ways:

Public void go ()

{

Mystruct x = new mystruct ();

Dosomething (ref X );

}

Public struct mystruct

{

Long A, B, C, D, E, F, G, H, I, J, K, L, M;

}

Public void dosomething (ref mystruct pvalue)

{

// Do something here ....

}

In this way, we can improve the efficiency of object allocation in memory.

The only thing to note is that when we pass the value type through reference, we will modify the value of this value type, that is, changing the pvalue value will cause a change in the value of X. Run the following code to convert the result to "123456" because the memory space actually pointed to by pvalue is consistent with the memory space declared by the X variable.

Public void go ()

{

Mystruct x = new mystruct ();

X. A = 5;

Dosomething (ref X );

Console. writeline (X. A. tostring ());

}

Public void dosomething (ref mystruct pvalue)

{

Pvalue. A = 12345;

}

* Transfer a reference type

Passing a reference type parameter is similar to passing a value type through reference in the previous example.

If we use the reference type:

Public class Myint

{

Public int myvalue;

}

Then call the go () method, and the Myint object will be placed on the stack:

Public void go ()

{

Myint x = new Myint ();

}

If we execute the following go () method:

Public void go ()

{

Myint x = new Myint ();

X. myvalue = 2;

Dosomething (X );

Console. writeline (X. myvalue. tostring ());

}

Public void dosomething (Myint pvalue)

{

Pvalue. myvalue = 12345;

}

This will happen...

1. Method go () into Stack
2. The variable X in the go () method is added to the stack.
3. Method dosomething ()
4. pvalue
5. The value of X (the pointer address of the Myint object in the stack) is copied to pvalue.

Therefore, when we change the myvalue member value of the Myint object in the heap through the pvalue of the Myint type, we then use another reference X pointing to the object to obtain its myvalue member value, the value is 12345 ".

What's more interesting is what happens when we pass a reference type through reference?

Let's test it. Assume that we have a "thing" class and two "animal" and "vegetable" classes inherited from "thing:

Public class thing

{

}

Public class animal: thing

{

Public int weight;

}

Public class vegetable: thing

{

Public int length;

}

Then execute the following go () method:

Public void go ()

{

Thing x = new animal ();

Switcharoo (ref X );

Console. writeline (

"X is animal :"

+ (X is animal). tostring ());

Console. writeline (

"X is vegetable :"

+ (X is vegetable). tostring ());

}

Public void switcharoo (ref thing pvalue)

{

Pvalue = new vegetable ();

}

Variable X is returned as the vegetable type.

X is animal: false
X is vegetable: True

Let's see what happened:

1. Go () method into Stack
2. x pointer into Stack
3. Animal objects are instantiated into the heap
4. switcharoo () method into Stack
5. pvalue goes into the stack and points to X

6. the vegetable object is instantiated into the heap.
7. The value of X is changed by the pvalue value pointing to the address of the vegetable object.

If we do not use the reference of thing, The result variable X will be of the animal type.

If the above Code does not make any sense to you, please continue to look at meArticleSo that you can have a better understanding of how variables of the reference type work.

We can see how the memory processes parameter passing. In the next part of the series, we will see what happened to the referenced variable in the stack, then consider how we solve some problems when copying objects.

To be continued...

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.