{"id":248362,"date":"2022-07-31T08:20:17","date_gmt":"2022-07-31T00:20:17","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=248362"},"modified":"2022-07-22T11:22:51","modified_gmt":"2022-07-22T03:22:51","slug":"c-unsafe-code","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/c-unsafe-code.html","title":{"rendered":"\u4e86\u89e3\u4e0bC# \u4e0d\u5b89\u5168\u4ee3\u7801"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5f53\u4e00\u4e2a\u4ee3\u7801\u5757\u4f7f\u7528 unsafe \u4fee\u9970\u7b26\u6807\u8bb0\u65f6\uff0cC# \u5141\u8bb8\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u6307\u9488\u53d8\u91cf\u3002\u4e0d\u5b89\u5168\u4ee3\u7801\u6216\u975e\u6258\u7ba1\u4ee3\u7801\u662f\u6307\u4f7f\u7528\u4e86\u6307\u9488\u53d8\u91cf\u7684\u4ee3\u7801\u5757\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u6307\u9488\u53d8\u91cf<\/strong><\/div>\n

\u6307\u9488 \u662f\u503c\u4e3a\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740\u7684\u53d8\u91cf\uff0c\u5373\uff0c\u5185\u5b58\u4f4d\u7f6e\u7684\u76f4\u63a5\u5730\u5740\u3002\u5c31\u50cf\u5176\u4ed6\u53d8\u91cf\u6216\u5e38\u91cf\uff0c\u60a8\u5fc5\u987b\u5728\u4f7f\u7528\u6307\u9488\u5b58\u50a8\u5176\u4ed6\u53d8\u91cf\u5730\u5740\u4e4b\u524d\u58f0\u660e\u6307\u9488\u3002<\/p>\n

\u6307\u9488\u53d8\u91cf\u58f0\u660e\u7684\u4e00\u822c\u5f62\u5f0f\u4e3a\uff1a<\/p>\n

type* var-name;<\/pre>\n

\u4e0b\u9762\u662f\u6307\u9488\u7c7b\u578b\u58f0\u660e\u7684\u5b9e\u4f8b\uff1a<\/p>\n\n\n\n\n\n\n\n\n\n\n\n
\u5b9e\u4f8b<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n
int* p<\/code><\/td>\np<\/code> \u662f\u6307\u5411\u6574\u6570\u7684\u6307\u9488\u3002<\/span><\/td>\n<\/tr>\n
double* p<\/code><\/td>\np<\/code> \u662f\u6307\u5411\u53cc\u7cbe\u5ea6\u6570\u7684\u6307\u9488\u3002<\/span><\/td>\n<\/tr>\n
float* p<\/code><\/td>\np<\/code> \u662f\u6307\u5411\u6d6e\u70b9\u6570\u7684\u6307\u9488\u3002<\/span><\/td>\n<\/tr>\n
int** p<\/code><\/td>\np<\/code> \u662f\u6307\u5411\u6574\u6570\u7684\u6307\u9488\u7684\u6307\u9488\u3002<\/span><\/td>\n<\/tr>\n
int*[] p<\/code><\/td>\np<\/code> \u662f\u6307\u5411\u6574\u6570\u7684\u6307\u9488\u7684\u4e00\u7ef4\u6570\u7ec4\u3002<\/span><\/td>\n<\/tr>\n
char* p<\/code><\/td>\np<\/code> \u662f\u6307\u5411\u5b57\u7b26\u7684\u6307\u9488\u3002<\/span><\/td>\n<\/tr>\n
void* p<\/code><\/td>\np<\/code> \u662f\u6307\u5411\u672a\u77e5\u7c7b\u578b\u7684\u6307\u9488\u3002<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5728\u540c\u4e00\u4e2a\u58f0\u660e\u4e2d\u58f0\u660e\u591a\u4e2a\u6307\u9488\u65f6\uff0c\u661f\u53f7 * \u4ec5\u4e0e\u57fa\u7840\u7c7b\u578b\u4e00\u8d77\u5199\u5165\uff1b\u800c\u4e0d\u662f\u7528\u4f5c\u6bcf\u4e2a\u6307\u9488\u540d\u79f0\u7684\u524d\u7f00\u3002 \u4f8b\u5982:<\/p>\n

int* p1, p2, p3;     \/\/ \u6b63\u786e  \r\nint *p1, *p2, *p3;   \/\/ \u9519\u8bef <\/pre>\n

\u4e0b\u9762\u7684\u5b9e\u4f8b\u8bf4\u660e\u4e86 C# \u4e2d\u4f7f\u7528\u4e86 unsafe \u4fee\u9970\u7b26\u65f6\u6307\u9488\u7684\u4f7f\u7528\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

using System;\r\nnamespace UnsafeCodeApplication\r\n{\r\n    class Program\r\n    {\r\n        static unsafe void Main(string[] args)\r\n        {\r\n            int var = 20;\r\n            int* p = &var;\r\n            Console.WriteLine(\"Data is: {0} \",  var);\r\n            Console.WriteLine(\"Address is: {0}\",  (int)p);\r\n            Console.ReadKey();\r\n        }\r\n    }\r\n}<\/pre>\n

\u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

Data is: 20\r\nAddress is: 99215364<\/pre>\n

\u60a8\u4e5f\u53ef\u4ee5\u4e0d\u7528\u58f0\u660e\u6574\u4e2a\u65b9\u6cd5\u4f5c\u4e3a\u4e0d\u5b89\u5168\u4ee3\u7801\uff0c\u53ea\u9700\u8981\u58f0\u660e\u65b9\u6cd5\u7684\u4e00\u90e8\u5206\u4f5c\u4e3a\u4e0d\u5b89\u5168\u4ee3\u7801\u3002\u4e0b\u9762\u7684\u5b9e\u4f8b\u8bf4\u660e\u4e86\u8fd9\u70b9\u3002<\/p>\n

\u4f7f\u7528\u6307\u9488\u68c0\u7d22\u6570\u636e\u503c<\/strong><\/div>\n

\u60a8\u53ef\u4ee5\u4f7f\u7528 ToString() \u65b9\u6cd5\u68c0\u7d22\u5b58\u50a8\u5728\u6307\u9488\u53d8\u91cf\u6240\u5f15\u7528\u4f4d\u7f6e\u7684\u6570\u636e\u3002\u4e0b\u9762\u7684\u5b9e\u4f8b\u6f14\u793a\u4e86\u8fd9\u70b9\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

using System;\r\nnamespace UnsafeCodeApplication\r\n{\r\n   class Program\r\n   {\r\n      public static void Main()\r\n      {\r\n         unsafe\r\n         {\r\n            int var = 20;\r\n            int* p = &var;\r\n            Console.WriteLine(\"Data is: {0} \" , var);\r\n            Console.WriteLine(\"Data is: {0} \" , p->ToString());\r\n            Console.WriteLine(\"Address is: {0} \" , (int)p);\r\n         }\r\n         Console.ReadKey();\r\n      }\r\n   }\r\n}<\/pre>\n

\u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

Data is: 20\r\nData is: 20\r\nAddress is: 77128984<\/pre>\n
\u4f20\u9012\u6307\u9488\u4f5c\u4e3a\u65b9\u6cd5\u7684\u53c2\u6570<\/strong><\/div>\n

\u60a8\u53ef\u4ee5\u5411\u65b9\u6cd5\u4f20\u9012\u6307\u9488\u53d8\u91cf\u4f5c\u4e3a\u65b9\u6cd5\u7684\u53c2\u6570\u3002\u4e0b\u9762\u7684\u5b9e\u4f8b\u8bf4\u660e\u4e86\u8fd9\u70b9\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

using System;\r\nnamespace UnsafeCodeApplication\r\n{\r\n   class TestPointer\r\n   {\r\n      public unsafe void swap(int* p, int *q)\r\n      {\r\n         int temp = *p;\r\n         *p = *q;\r\n         *q = temp;\r\n      }\r\n\r\n      public unsafe static void Main()\r\n      {\r\n         TestPointer p = new TestPointer();\r\n         int var1 = 10;\r\n         int var2 = 20;\r\n         int* x = &var1;\r\n         int* y = &var2;\r\n         \r\n         Console.WriteLine(\"Before Swap: var1:{0}, var2: {1}\", var1, var2);\r\n         p.swap(x, y);\r\n\r\n         Console.WriteLine(\"After Swap: var1:{0}, var2: {1}\", var1, var2);\r\n         Console.ReadKey();\r\n      }\r\n   }\r\n}<\/pre>\n

\u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

Before Swap: var1: 10, var2: 20\r\nAfter Swap: var1: 20, var2: 10<\/pre>\n
\u4f7f\u7528\u6307\u9488\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20<\/strong><\/div>\n

\u5728 C# \u4e2d\uff0c\u6570\u7ec4\u540d\u79f0\u548c\u4e00\u4e2a\u6307\u5411\u4e0e\u6570\u7ec4\u6570\u636e\u5177\u6709\u76f8\u540c\u6570\u636e\u7c7b\u578b\u7684\u6307\u9488\u662f\u4e0d\u540c\u7684\u53d8\u91cf\u7c7b\u578b\u3002\u4f8b\u5982\uff0cint *p \u548c int[] p \u662f\u4e0d\u540c\u7684\u7c7b\u578b\u3002\u60a8\u53ef\u4ee5\u589e\u52a0\u6307\u9488\u53d8\u91cf p\uff0c\u56e0\u4e3a\u5b83\u5728\u5185\u5b58\u4e2d\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u662f\u6570\u7ec4\u5730\u5740\u5728\u5185\u5b58\u4e2d\u662f\u56fa\u5b9a\u7684\uff0c\u6240\u4ee5\u60a8\u4e0d\u80fd\u589e\u52a0\u6570\u7ec4 p\u3002<\/p>\n

\u56e0\u6b64\uff0c\u5982\u679c\u60a8\u9700\u8981\u4f7f\u7528\u6307\u9488\u53d8\u91cf\u8bbf\u95ee\u6570\u7ec4\u6570\u636e\uff0c\u53ef\u4ee5\u50cf\u6211\u4eec\u901a\u5e38\u5728 C \u6216 C++ \u4e2d\u6240\u505a\u7684\u90a3\u6837\uff0c\u4f7f\u7528 fixed \u5173\u952e\u5b57\u6765\u56fa\u5b9a\u6307\u9488\u3002<\/p>\n

\u4e0b\u9762\u7684\u5b9e\u4f8b\u6f14\u793a\u4e86\u8fd9\u70b9\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

using System;\r\nnamespace UnsafeCodeApplication\r\n{\r\n   class TestPointer\r\n   {\r\n      public unsafe static void Main()\r\n      {\r\n         int[]  list = {10, 100, 200};\r\n         fixed(int *ptr = list)\r\n\r\n         \/* \u663e\u793a\u6307\u9488\u4e2d\u6570\u7ec4\u5730\u5740 *\/\r\n         for ( int i = 0; i < 3; i++)\r\n         {\r\n            Console.WriteLine(\"Address of list[{0}]={1}\",i,(int)(ptr + i));\r\n            Console.WriteLine(\"Value of list[{0}]={1}\", i, *(ptr + i));\r\n         }\r\n         Console.ReadKey();\r\n      }\r\n   }\r\n}<\/pre>\n

\u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

Address of list[0] = 31627168\r\nValue of list[0] = 10\r\nAddress of list[1] = 31627172\r\nValue of list[1] = 100\r\nAddress of list[2] = 31627176\r\nValue of list[2] = 200<\/pre>\n
\u7f16\u8bd1\u4e0d\u5b89\u5168\u4ee3\u7801<\/strong><\/div>\n

\u4e3a\u4e86\u7f16\u8bd1\u4e0d\u5b89\u5168\u4ee3\u7801\uff0c\u60a8\u5fc5\u987b\u5207\u6362\u5230\u547d\u4ee4\u884c\u7f16\u8bd1\u5668\u6307\u5b9a \/unsafe \u547d\u4ee4\u884c\u3002<\/p>\n

\u4f8b\u5982\uff0c\u4e3a\u4e86\u7f16\u8bd1\u5305\u542b\u4e0d\u5b89\u5168\u4ee3\u7801\u7684\u540d\u4e3a prog1.cs \u7684\u7a0b\u5e8f\uff0c\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u547d\u4ee4\uff1a<\/p>\n

csc \/unsafe prog1.cs<\/pre>\n

\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f Visual Studio IDE\uff0c\u90a3\u4e48\u60a8\u9700\u8981\u5728\u9879\u76ee\u5c5e\u6027\u4e2d\u542f\u7528\u4e0d\u5b89\u5168\u4ee3\u7801\u3002<\/p>\n

\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n

    \n
  1. \u901a\u8fc7\u53cc\u51fb\u8d44\u6e90\u7ba1\u7406\u5668\uff08Solution Explorer\uff09\u4e2d\u7684\u5c5e\u6027\uff08properties\uff09\u8282\u70b9\uff0c\u6253\u5f00\u9879\u76ee\u5c5e\u6027\uff08project properties\uff09\u3002<\/li>\n
  2. \u70b9\u51fb Build \u6807\u7b7e\u9875\u3002<\/li>\n
  3. \u9009\u62e9\u9009\u9879\"Allow unsafe code\"\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"

    \u6307\u9488 \u662f\u503c\u4e3a\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740\u7684\u53d8\u91cf\uff0c\u5373\uff0c\u5185\u5b58\u4f4d\u7f6e\u7684\u76f4\u63a5\u5730\u5740\u3002\u5c31\u50cf\u5176\u4ed6\u53d8\u91cf\u6216\u5e38\u91cf\uff0c\u60a8\u5fc5\u987b\u5728\u4f7f\u7528\u6307\u9488\u5b58\u50a8\u5176\u4ed6\u53d8\u91cf\u5730 […]<\/p>\n","protected":false},"author":321,"featured_media":248364,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-248362","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/248362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=248362"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/248362\/revisions"}],"predecessor-version":[{"id":248367,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/248362\/revisions\/248367"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/248364"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=248362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=248362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=248362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}