zoukankan      html  css  js  c++  java
  • C# 可否对内存进行直接的操作?

    可以,用 unsafe。用的时候记得在项目属性(Properties)->生成(Build)->常规(General)中钩上允许不安全代码 (Allow unsafe code)。
    否则会出现这个错误:Unsafe code may only appear if compiling with /unsafe。

    // compile with: /unsafe

    using System;
    class UnsafeTest
    {
    // Unsafe method: takes pointer to int:
    unsafe static void SquarePtrParam(int* p)
    {
    *p *= *p;
    }

    unsafe static void Main()
    {
    int i = 5;
    // Unsafe method: uses address-of operator (&):
    SquarePtrParam(&i);
    Console.WriteLine(i);
    }
    }
    // Output: 25

    复制代码
    // compile with: /unsafe
    
    using System;
    class UnsafeTest
    {
        // Unsafe method: takes pointer to int:
        unsafe static void SquarePtrParam(int* p)
        {
            *p *= *p;
        }
    
        unsafe static void Main()
        {
            int i = 5;
            // Unsafe method: uses address-of operator (&):
            SquarePtrParam(&i);
            Console.WriteLine(i);
        }
    }
    // Output: 25
    复制代码
    复制代码
    // compile with: /unsafe
    
    using System;
    class UnsafeTest
    {
        // Unsafe method: takes pointer to int:
        unsafe static void SquarePtrParam(int* p)
        {
            *p *= *p;
        }
    
        unsafe static void Main()
        {
            int i = 5;
            // Unsafe method: uses address-of operator (&):
            SquarePtrParam(&i);
            Console.WriteLine(i);
        }
    }
    // Output: 25
    复制代码
  • 相关阅读:
    sklearn之线性回归
    机器学习概述
    scipy之定积分计算和简单图像处理
    scipy之插值器
    numpy之排序
    spring mvc 实现文件上传
    Maven安装本地jar包到本地仓库
    spring mvc实现转发和重定向
    sprign mvc 解决中文乱码问题
    spring mvc 中使用session
  • 原文地址:https://www.cnblogs.com/binyao/p/4891182.html
Copyright © 2011-2022 走看看