C # Unsafe code writing and pointer Application

Source: Internet
Author: User

Keywords: managedCodeManaged code, Insecure code Unsafe code, fixed hosting pointer fixed, allocate space on Stack stackalloc

Managedd code and unmanaged code
Code running under the control of the Common Language Runtime Library (CLR) is called "managed code", and code running outside the CLR is called "unmanaged code ". Com, COM +, C ++ components, ActiveX components, and Win32 APIs are examples of unmanaged code.

Concept 1: security code safe code and unsafe code Unsafe code
the so-called security code refers to the Code managed by CLR for security verification. In the security code, we cannot directly perform memory operations because all memory heap are managed heap ). However, in some applications, you still need to perform direct operations on the memory. In this case, you need to mark the code as Insecure code unsafe.
Insecure code is a code that cannot be authenticated by CLR. The security of the Code is ensured by the user to avoid security risks or pointer errors.
Any code that requires pointer operations must be marked as a non-secure code.
the keyword of Insecure code is unsafe.
note: in C #, /unsafe compile the application Program .

Concept 2: Fixed keyword Application
Under the managed framework, we know that the addresses of all reference type instances in the managed heap are not fixed and they will continue to recycle the memory as the GC continues, managed Objects must also be moved between generational Garbage Collector 0, 1 and 2. So how can we use pointers to a managed object if we write Insecure code?
The only way is to fix this object in the managed heap! This is our keyword fixed. That is, to fix the managed object to be pointed.
For example
Int [] A = new int [10];
Unsafe
{
Fixed (int * P = A) // fix!
{
Operate the pointer p ......
}
}

Concept 3: dynamically allocate space on the stack stackalloc
In the unsafe context, we can dynamically allocate memory blocks on the stack. Because the applied memory block does not belong to the managed heap and is not subject to GC, the lifetime of the memory block is limited by the lifetime of the method defined for it. Therefore, this memory block does not need to be Fixed (fixed)
For example:
Public unsafe void Test
{
Int * P = stackalloc int [100];
/*
On the stack (Note: not managed heap !! Remember)An array with a length of 100 integers is applied, and P points to the first address of the array.
*/
Int [] q = new int [100];
/*
Q is the space applied for on the managed heap. If a pointer points to Q at this time, it must be fixed.
Fixed (int * P1 = q)
{
}
*/
}

Concept 4: Various pointer types in C #

example description

int * P

P is a pointer to an integer

int ** P

P is a pointer to an integer

int * [] P

P is a one-dimensional array pointing to an integer pointer

char * P

P is a pointer to a character

void * P

P is a pointer to an unknown type

Related Article

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.