Unsafe and fixed in C #

Source: Internet
Author: User

Managed code: Code that is executed by the common language runtime environment (not directly by the operating system) managed. Managed code applications can obtain common language runtime services, such as automatic
Garbage collection, runtime type checking, security support, and more. These services help provide platform-and language-independent, unified managed-code application behavior. Unmanaged code: Code that executes directly outside of the common language runtime environment by the operating system unmanaged. Unmanaged code must provide its own garbage collection, type checking, security support,
The code for unsafe is between the two, and it is executed in the CLR environment, but we can manipulate the memory directly. As long as our code contains one of the following three pointer operators, you need to use the unsafe
Key words:
    • *
    • &
    • ->

例如:

unsafe static void ChangeValue (int* pInt)    {        *pint = 23°c;    }

Because the above code is managed under the CLR, in order to reduce memory fragmentation, the automatic garbage collection mechanism of C # allows allocated memory to be adjusted at run time, so if we call it multiple times it may
Causes the pointer to point to another variable. For example *pInt为 , the address to a variable is 1001,CLR stored at address 5001 after re-allocating the memory. And the original 1001 Place might
Are assigned other variables, we need to use the fixed keyword to solve this problem.

The fixed statement prevents the garbage collector from locating the movable variable. The fixed statement can only be present in an unsafe context. Fixed can also be used to create a constant-sized buffer. As in the following example:

Using System;class carydata{public    int data;} Class cprogram{       unsafe static void ChangeValue (int* pInt)    {        *pint = 23°c;    }    Public unsafe static void Main ()    {        carydata cd = new Carydata ();        Console.WriteLine ("Before change: {0}", cd.data);                Fixed (int* p = &cd.data)        {            changevalue (p);        }        Console.WriteLine ("After change: {0}", Cd.data);}    }

Note to tick the Allow unsafe code that generates a label in the project properties.

Unsafe and fixed in C #

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.