c# 使用c++指针

  c++指针基本使用需要指定unsafe编译选项。

using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication1
{
    internal class Program
    {
        unsafe private static void Main(string[] args)
        {
            int x = 10;
            short y = -1;
            byte y2 = 4;
            double z = 1.5;
            int* p = &x;
            short* py = &y;
            double* pz = &z;

            Console.WriteLine("x {0},{1},{2}", (uint)&x, sizeof(int), x);
            Console.WriteLine("y {0},{1},{2}", (uint)&y, sizeof(short), y);
            Console.WriteLine("y2 {0},{1},{2}", (uint)&y2, sizeof(byte), y2);
            Console.WriteLine("z {0},{1},{2}", (uint)&z, sizeof(double), z);
            Console.WriteLine("p {0},{1},{2}", (uint)&p, sizeof(int*), (uint)p);
            Console.WriteLine("py {0},{1},{2}", (uint)&py, sizeof(short*), (uint)py);
            Console.WriteLine("pz {0},{1},{2}", (uint)&pz, sizeof(double*), (uint)pz);

            *p = 20;
            Console.WriteLine("x {0}", x);
            Console.WriteLine("p {0}", *p);

            //强制转换后结果不确定 这个和unsafe没有关系 c++的指针本来就这不能这样操作
            pz = (double*)p;
            Console.WriteLine("pz {0}",*pz);
        }
    }
}

  结构体为非托管类型,类为托管类型,类的地址会由于gc的操作发生改变,需要使用fixed。

using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication1
{
    internal class Program
    {
        internal struct CurrencyStruct
        {
            public long dollars;
            public byte cents;

            public override string ToString()
            {
                return string.Format("Doolars: {0}, Cents: {1}", dollars, cents);
            }
        }

        internal class CurrenyClass
        {
            public long dollars;
            public byte cents;

            public override string ToString()
            {
                return string.Format("Dollars: {0}, Cents: {1}", dollars, cents);
            }
        }



        unsafe private static void Main(string[] args)
        {
            Console.WriteLine(sizeof(CurrencyStruct));

            CurrencyStruct amount1, amount2;
            CurrencyStruct* pAmount = &amount1;
            long* pDollars = &(pAmount->dollars);
            byte* pCents = &(pAmount->cents);

            Console.WriteLine("{0}", (uint)&amount1);
            Console.WriteLine("{0}", (uint)&amount2);
            Console.WriteLine("{0}", (uint)&pAmount);
            Console.WriteLine("{0}", (uint)&pDollars);
            Console.WriteLine("{0}", (uint)&pCents);

            pAmount->dollars = 20;
            *pCents = 50;
            Console.WriteLine(amount1);

            //指向amount2
            pAmount--;
            Console.WriteLine("{0}, {1}", (uint)pAmount, *pAmount);

            //指向amount2的pcents
            CurrencyStruct* pTempCurrency = (CurrencyStruct*) pCents;
            pCents = (byte*) (--pTempCurrency);
            Console.WriteLine("{0}", (uint)&pCents);

            CurrenyClass amount3 = new CurrenyClass();

            fixed(long* pDollar3 = &(amount3.dollars))
            fixed(byte* pCents3 = &(amount3.cents))
            {
                Console.WriteLine("{0}", (uint)pDollar3);
                Console.WriteLine("{0}", (uint)pCents3);

                *pDollar3 = -100;
                Console.WriteLine("{0}", amount3);
            }


        }
    }
}

在栈中创建高性能、低开销的数组

  

using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication1
{
    internal class Program
    {

        private const int Len = 40;
        unsafe private static void Main(string[] args)
        {
            long* pArray = stackalloc long[Len];

            for (int i = 0; i < Len; i++)
            {
                pArray[i] = i * i;
            }


            for (int i = 0; i < Len; i++)
            {
                Console.WriteLine("{0}", pArray[i]);
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/zkzk945/p/5135994.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSharp 调用C++ DLL; 参数为指针类型导出函数 c# Csharp调用 c++库 参数为导入和导出指针两种 包含C++ DLL源码 如fun(cont char* A,char*B) A为输入参数,B为输出参数-C# CSharp call C++ DLL lib dll function param use export and import eg: fun(cont char* A,char*B) A IN,B OUT TestDll\Debug\TestCallDll.exe .......\.....\TestCallDll.vshost.exe .......\.....\TestCallDll.vshost.exe.manifest .......\.....\TestDll.dll .......\.....\TestDll.lib .......\TestCallDll\Form1.cs .......\...........\Form1.Designer.cs .......\...........\Form1.resx .......\...........\obj\Debug\TestCallDll.csproj.FileListAbsolute.txt .......\...........\...\.....\TestCallDll.csproj.GenerateResource.Cache .......\...........\...\.....\TestCallDll.exe .......\...........\...\.....\TestCallDll.Form1.resources .......\...........\...\.....\TestCallDll.pdb .......\...........\...\.....\TestCallDll.Properties.Resources.resources .......\...........\Program.cs .......\...........\...perties\AssemblyInfo.cs .......\...........\..........\Resources.Designer.cs .......\...........\..........\Resources.resx .......\...........\..........\Settings.Designer.cs .......\...........\..........\Settings.settings .......\...........\TestCallDll.csproj .......\....Dll\dllmain.cpp .......\.......\ReadMe.txt .......\.......\stdafx.cpp .......\.......\stdafx.h .......\.......\targetver.h .......\.......\TestDll.cpp .......\.......\TestDll.def .......\.......\TestDll.h .......\.......\TestDll.vcproj .......\.......\TestDll.vcproj.PC-201008261742.Administrator.user .......\TestDll.sln .......\TestDll.suo .......\....CallDll\obj\Debug\TempPE .......\...........\...\Debug .......\...........\obj .......\...........\Properties .......\Debug .......\TestCallDll .......\TestDll TestDll
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值