zoukankan      html  css  js  c++  java
  • Unsafe Code

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 
      7 namespace UnsafeCode
      8 {  
      9     struct Point
     10     {
     11         public int x;
     12         public int y;
     13 
     14         public override string ToString()
     15         {
     16             return string.Format("({0}, {1})", x, y);
     17         }
     18     }
     19 
     20     class PointRef  
     21     {
     22         public int x;
     23         public int y;
     24         public override string ToString()
     25         {
     26             return string.Format("({0}, {1})", x, y);
     27         }
     28     }
     29 
     30     class Program
     31     {
     32         static void Main( string[] args )
     33         {
     34             Console.WriteLine("***** Calling method with unsafe code *****");
     35       
     36             int i = 10, j = 20;
     37 
     38             // 安全交换
     39             Console.WriteLine("
    ***** Safe swap *****");
     40             Console.WriteLine("Values before safe swap: i = {0}, j = {1}", i, j);
     41             SafeSwap(ref i, ref j);
     42             Console.WriteLine("Values after safe swap: i = {0}, j = {1}", i, j);
     43 
     44             // 不安全交换
     45             Console.WriteLine("
    ***** Unsafe swap *****");
     46             Console.WriteLine("Values before unsafe swap: i = {0}, j = {1}", i, j);
     47             unsafe { UnsafeSwap(&i, &j); }
     48             Console.WriteLine("Values after unsafe swap: i = {0}, j = {1}", i, j);
     49 
     50             UsePointerToPoint();
     51             PrintValueAndAddress();
     52             UseAndPinPoint();
     53             UseSizeOfOperator();
     54 
     55             Console.ReadLine();
     56         }
     57 
     58         unsafe static void UsePointerToPoint()
     59         {
     60             // 指针访问成员
     61             Point point;
     62             Point* p = &point;
     63             p->x = 100;
     64             p->y = 200;
     65             Console.WriteLine(p->ToString());
     66 
     67             // 指针间接寻址访问成员
     68             Point point2;
     69             Point* p2 = &point2;
     70             (*p2).x = 100;
     71             (*p2).y = 200;
     72             Console.WriteLine((*p2).ToString());
     73         }
     74 
     75         unsafe static void UnsafeStackAlloc()
     76         {
     77             char* p = stackalloc char[256];
     78             for (int k = 0; k < 256; k++)
     79                 p[k] = (char)k;
     80         }
     81 
     82         // 静态方法或实例方法均可被标记为不安全
     83         unsafe static void SquareIntPointer( int* myIntPointer )
     84         {
     85             *myIntPointer *= *myIntPointer;
     86         }
     87 
     88         unsafe static void PrintValueAndAddress()
     89         {
     90             int myInt;          
     91             int* ptrToMyInt = &myInt;
     92 
     93             // 指针间接寻址为myInt赋值
     94             *ptrToMyInt = 123;
     95    
     96             Console.WriteLine("Value of myInt {0}", myInt);
     97             Console.WriteLine("Address of myInt {0:X}", (int)&ptrToMyInt);
     98         }
     99 
    100         unsafe public static void UnsafeSwap( int* i, int* j )
    101         {
    102             int temp = *i;
    103             *i = *j;
    104             *j = temp;
    105         }
    106 
    107         unsafe public static void UseAndPinPoint()
    108         {
    109             PointRef pt = new PointRef();
    110             pt.x = 5;
    111             pt.y = 6;
    112           
    113             // fixed 语句设置指向托管类型的指针并在代码执行过程中固定该变量
    114             // 固定pt
    115             fixed (int* p = &pt.x)
    116             {
    117                 // 此处可使用 int* 变量
    118             }
    119 
    120             // pt未固定,该方法完成后可被GC清除
    121             Console.WriteLine("Point is: {0}", pt);
    122         }
    123 
    124         unsafe static void UseSizeOfOperator()
    125         {// 不安全关键字 sizeof
    126             Console.WriteLine("The size of short is {0}.", sizeof(short));
    127             Console.WriteLine("The size of int is {0}.", sizeof(int));
    128             Console.WriteLine("The size of long is {0}.", sizeof(long));
    129             Console.WriteLine("The size of Point is {0}.", sizeof(Point));
    130         }
    131 
    132         public static void SafeSwap( ref int i, ref int j )
    133         {
    134             int temp = i;
    135             i = j;
    136             j = temp;
    137         }
    138     }
    139 }
  • 相关阅读:
    PAT 甲级 1126 Eulerian Path (25 分)
    PAT 甲级 1126 Eulerian Path (25 分)
    PAT 甲级 1125 Chain the Ropes (25 分)
    PAT 甲级 1125 Chain the Ropes (25 分)
    PAT 甲级 1124 Raffle for Weibo Followers (20 分)
    PAT 甲级 1124 Raffle for Weibo Followers (20 分)
    PAT 甲级 1131 Subway Map (30 分)
    PAT 甲级 1131 Subway Map (30 分)
    AcWing 906. 区间分组 区间贪心
    AcWing 907. 区间覆盖 区间贪心
  • 原文地址:https://www.cnblogs.com/ht-beyond/p/4473941.html
Copyright © 2011-2022 走看看