zoukankan      html  css  js  c++  java
  • 不安全代码

    第一次使用unsafe:

    为什么会出现按这种情况?

      因为C#中的unsafe是用来声明不安全代码,所以编译不通过。(当然,所谓的不安全只是对于我这种渣渣来说的)。

    使用unsafe的好处就是:可以想C++那样使用指针来提高性能;还有就是查看内存状况,例如简单点的就是查看指针。

    不好的就是:最严重的就是内存泄漏。

    注:最好别随意使用,反正作为渣渣的我现在还不会去用,这里做随笔只是记录有这个东西,去学习这个东西。

    怎么解决这种错误呢?

    在"项目"里面有一个和文件的名字相同的一个属性设置,点开它

    进入到"生成",然后勾上不安全代码这一项。然后就可以使用了

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Collections;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            // 整个方法声明
            static unsafe void Main(string[] args)
            {
                int var = 20;
                int* p = &var;
                Console.WriteLine(var);
                Console.WriteLine((int)p);
                Console.WriteLine(*p);
                Console.ReadKey();
            }
            public void my_func()
            {
                unsafe
                {
                    // 部分不安全代码声明
                }
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Collections;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static unsafe void Main(string[] args)
            {
                int[] list1 = { 1, 2, 3, 4, 5, 6 };
                // 固定指正来的(注:没有分号)
                fixed (int *ptr = list1)
                for (int i = 0; i < list1.Length; i++)
                {
                    Console.WriteLine("指针(地址):{0}", (int)(ptr + i));
                    Console.WriteLine("内容:{0}", *(ptr + i));
                }
            }
        }
    }
  • 相关阅读:
    WebUploader大文件上传支持分场上传
    WebUploader大文件上传支持切片上传
    WebUploader大文件上传支持分片上传
    ASP.NET大文件上传支持切割上传
    ASP.NET大文件上传支持分场上传
    ASP.NET大文件上传支持切片上传
    ASP.NET大文件上传支持分片上传
    C#大文件上传支持切割上传
    C#大文件上传支持分场上传
    山中何太冷,自古非今年
  • 原文地址:https://www.cnblogs.com/namejr/p/10486342.html
Copyright © 2011-2022 走看看