高手解答直接插入排问题

来源:百度知道 编辑:UC知道 时间:2024/06/06 18:03:06
程序有些问题
1)结果不对
2)运行提示stack around the variable “a”was corrupted
高手解答:
#include <iostream>
using namespace std;
void sort (int length,int *p)
{
int x=0;
for (int i=1;i<=length;++i)
{
x=p[i];
int j=0;
for ( j=i-1;p[j]>x&&j;j--)
{
p[j+1]=p[j];
}
p[j]=x;

}
}
int main()
{
int a[]={5,4,3,2,1,9,8,7,6,0};
int l=sizeof(a)/sizeof(int);
sort(l,a);
for (int i=0;i<9;++i)
{
cout<<a[i]<<" ";
}
return 0;

}
运行时检测失败 - 变量a周围的栈被破坏
这个解释我用msdn也知道了,
但是具体原因是什么呢,能说清楚点吗?这个程序并没有写入许多内容啊

楼上高手啊,学习啦!
程序的具体错误应该是数组越界了,l=10,数组下标为0~9,所以sort函数要改一下

运行时检测失败 - 变量a周围的栈被破坏

当你试图向一个特定大小的内存中写入许多数据时,就会引起这个问题,通常是你向一个固定大小的字符串缓存中写入过多的内容引起。
i.e.
int _tmain(int argc, _TCHAR* argv[])
{
char mybuf[10];
strcpy(mybuf, "This is definitely more than 10 characters long, it will also cause a Run-Time Check");
}

我用vc++6.0运行可以通过,运行结果是066778899