Non-secure programming in C #

xiaoxiao2021-03-20  203

This is a topic of C / C pruning often talks, and it is also a complex, unintererated topic - pointer! Every time I talk about C #, most of the people I have encountered have such a view - C # There is no pointer in C #. In fact, it has been abolished, and it is replaced by non-secure programming in C # - how to use pointers in the program. Unlike it, it is not safe to use the pointer programming.

It is so concerned that the non-secure programming is different from habitual .NET development specification, and requires programmers to clearly determine local environment settings (only for local execution only). This article I will discuss non-secure programming this topic from the difference between the two most easily doubtful concepts - non-security code and non-controlled code. Next we will discuss how to write non-secure codes, which is how to use pointers in C #.

Is non-safe or non-controlled?

The controlled code refers to the code executed under the CLR management. The CLR is responsible for many behind-the-scenes:

Management object's memory

Type verification

Garbage collection

Say these, it is actually to free users from these work, concentrate on business implementation. Users no longer need to perform memory operations directly because these work has been completed by the CLR.

On the other hand, non-controlled code is the code that executed in the CLR context. The best example is our usual WIN32 DLL, such as kernel32.dll, user32.dll, and various COM components on our system. How to allocate memory, how to release these memory, how to implement type authentication? These work require them to do itself. A typical C program assigns a statement of a character pointer is another example of a non-controlled code, because as a programmer, you have to be responsible:

Call the memory allocation function to ensure that the result of the type conversion correctly ensures that the memory is released after the use is complete.

If you pay attention to the above explanation, all of these tasks are completed by the CLR to mitigate the burden of the programmer.

Non-secure code is a code type between controlled and non-controlled code

Non-secure codes are still acting as controlled by the control code, but it is allowed to directly access memory directly like non-controlled code. So you get the advantages of both. If you are writing a .NET application, but also hopes that you can use various functions in Win32 DLL - you need to use a pointer, then the non-secure code is your savior.

After we have clarified the difference between the two, we start writing the actual code, there is no doubt that this is the most exciting part. What are you thinking about? In-depth non-security code

Write non-secure codes to use special keywords Unsafe with Fixed. If you still remember, there are three pointer operators:

* & ->

Any statement that uses any of the above pointers, the statement block or function is applied to the unsafe keyword as a non-security code, just like this:

Public UNSAFE VOID TrIPLE (INT * PINT) {* PINT = (* PINT) * 3;}

The above function is only twice the value of the incoming parameters. But please note that the incoming pointer is the pointer! Since this function uses the "*" operator to perform memory operations, it is marked as unsafe.

But there is still a problem here. Recall the above discussion, the non-secure code is also controlled by the controlled code under the CLR management, and the CLR can freely transfer objects into memory. So a similar, not, may cause memory leakage. The result of this is that for the programmer may point the pointer of this variable to other places in memory in consciously unconscious.

Therefore, it is assumed that the address pointing from PINT is 1001, and the CLR memory retransmission process will lead to memory leakage. Point 1001 before PINT, and the data indicated after the relocation may be stored at address 2003. So the big disaster! The data stored at 1001 point to PINT is invalid after the relocation process. This may be. Net rarely mention the reason for the use of pointers, what do you think? Fixed pointer

Enter keyword fixed before the statement block, will tell the object within the CLR block that cannot be relocated so that the CLR does not relocate the pointer points to the data storage location. Therefore, when using a pointer in the C #, use the keyword Fixed will prevent the generating of the program runtime. Let us see how it works:

Using system; class cdata {public int x;}

Class Cprogram {Unsafe Static Void SetVal (INT * PINT) {* PINT = 1979;} public unsafe static void main () {cdata d = new cdata (); console.writeline ("Previous Value: {0}", DX) Fixed (int * p = & d.x) {setval (p);} console.writeline ("new value: {0}", dx);}}

In this code, we pass a Fixed block in this code to assign the address of the CDATA object data (domain) X to an integer pointer P. When the statement in the Fixed block is executed, this pointer P will always point to the original memory area, because the CLR has been indicated to temporarily freeze this variable until the Fixed block is executed. Once the Fixed block is executed, this object can be repositioned by the CLR.

The above is the introduction of the pointer programming in C #. The key is to indicate that the block is unsafe and fixed. I hope to improve your knowledge of your pointer to C #!

转载请注明原文地址:https://www.9cbs.com/read-130412.html

New Post(0)