这段unsafe代码会不会造成内存泄露或者其他运行时问题?
vs2010+win7经测试,可以正常调用。疑惑rt,如果存在问题,该如何解决?谢谢。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace WindowsFormsApplication3//测试程序{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int ret= mycl.test(testarr.arr); for (int k = 0; k < testarr.arr.Length; ++k) { textBox1.Text = textBox1.Text + testarr.arr[k].ToString(); } public class mycl { [DllImport("testdll.dll")] public static extern int test(int[] arr);//testdll为c++写的win32 dll,里面有个test函数,对传入的数组进行初始化 } public class testarr { public unsafe static int[] arr = new int[100]; } }
unsafe struct testarr{ public fixed int arr[32]; //请留意括号'[]'的位置}