问题:编译,链接都没问题,运行结束时出现Debug error错误,DAMAGE:After Normal block (#187) at 0x00033160
源码:
//子函数问题,与主函数无关, 主函数的have[pn][rn],need[i][j],offer[rn]被传递,返回一整数值
int issafe(vector < vector<int> >& have, vector < vector<int> >& need, vector <int>& offer)
{
int safe=0;
vector < vector<int> >have1(pn);//全局变量:pn,rn
vector < vector<int> >need1(pn);
vector <int> offer1(rn);
for(int i=0; i<pn; i++)
{
for(int j=0; j<rn; j++)
{
have1[i][j]=have[i][j]; //have1[i].push_back(have[i][j]);
need1[i][j]=need[i][j];//need1[i].push_back(need[i][j]);
}
}
return safe;
}
原因:主要是对于动态分配的内存的错误使用!have1向量只知道包含pn个一维向量,而却不知道每个一维向量有多少个元素,因此have1[i][j]=have[i][j]; 是错误的;这样访问到错误的内存---那个地址的内容未知。
出现 debug error ,在于VC检测到了这个错误信息!--
解决:绿色部分改成红色部分
参考http://www.programfan.com/club/showpost.asp?id=146259&t=o: