主题:有关不安全代码的执行!
using System;
public class Test
{
unsafe public static void Sort(double *p)
{
double temp;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(p[j]>p[j+1])
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
unsafe public static int BinSearch(double *list,double key)
{
int low=0;
int high=10;
int mid =0;
double midvalue=0;
while(low<=high)
{
mid=(low+high)/2;
midvalue=list[mid];
if(key>midvalue)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
return low;
}
unsafe public static void Main()
{
double *d=stackalloc double[10];
double *p=d;
for(int i=0;i<10;i++)
{
Console.WriteLine ("please put {0}shu",i+1);
p[i]=double.Parse (Console.ReadLine ());
}
Sort(p);
Console.WriteLine ("put a number");
double ds=double.Parse (Console.ReadLine ());
int key=BinSearch(p,ds);
double *nd=stackalloc double[11];
double *np=nd;
for(int i=0;i<11;i++)
{
if(i<ds)
{
np[i]=p[i];
}
else if(i==key)
{
np[i]=ds;
}
else
np[i]=p[i-1];
}
for(int i=0;i<11;i++)
{
Console.WriteLine(np[i]);
}
}
}
为什么在执行的时候总是提示:不安全代码只会在使用unsafe中执行
public class Test
{
unsafe public static void Sort(double *p)
{
double temp;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(p[j]>p[j+1])
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
unsafe public static int BinSearch(double *list,double key)
{
int low=0;
int high=10;
int mid =0;
double midvalue=0;
while(low<=high)
{
mid=(low+high)/2;
midvalue=list[mid];
if(key>midvalue)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
return low;
}
unsafe public static void Main()
{
double *d=stackalloc double[10];
double *p=d;
for(int i=0;i<10;i++)
{
Console.WriteLine ("please put {0}shu",i+1);
p[i]=double.Parse (Console.ReadLine ());
}
Sort(p);
Console.WriteLine ("put a number");
double ds=double.Parse (Console.ReadLine ());
int key=BinSearch(p,ds);
double *nd=stackalloc double[11];
double *np=nd;
for(int i=0;i<11;i++)
{
if(i<ds)
{
np[i]=p[i];
}
else if(i==key)
{
np[i]=ds;
}
else
np[i]=p[i-1];
}
for(int i=0;i<11;i++)
{
Console.WriteLine(np[i]);
}
}
}
为什么在执行的时候总是提示:不安全代码只会在使用unsafe中执行