Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:为什么会Runtime Error?In Reply To:为什么会Runtime Error? Posted by:shuangkouchengzhi at 2014-04-29 16:41:56 > #include <iostream> > using namespace std; > void check(char**p,int,int); > int main() > { > int N,M; > cin>>M>>N; > char**p=new char*[N]; > for (int i=0;i<N;i++) p[i]=new char[M]; > for (int j=0;j<N+2;j++) > { > p[0][j]='.'; > p[M+1][j]='.'; > } > for (int i=1;i<M+1;i++) > { > p[i][0]='.'; > p[i][N+1]='.'; > } > for (int i=1;i<=M;i++) > for (int j=1;j<=N;j++) > cin>>p[i][j]; > int counter=0; > for(int i=1;i<=M;i++) > for(int j=1;j<=N;j++) > if(p[i][j]=='W') > { > counter++; > p[i][j]=1; > check(p,i,j); > } > cout<<counter<<endl; > for (int i=0;i<N;i++) delete[] p[i]; > delete[] p; > return 0; > > } > > void check(char**p,int hang,int lie) > { > bool flag=true; > while(flag) > { > flag=false; > if (p[hang-1][lie+1]=='W') > { > flag=true; > p[hang-1][lie+1]=1; > check(p,hang-1,lie+1); > } > for(int i=hang;i<=hang+1;i++) > for(int j=lie-1;j<=lie+1;j++) > { > if(p[i][j]=='W') > { > flag=true; > p[i][j]=1; > check(p,i,j); > } > } > } > if(flag==false) return; > > } > 因为你只给地图分配了N行,每行M个字符的内存大小,而实际情况是:每行的字符为M没错,但是还有一个'\0'字符,是必须有的,这么一来,每读入一行字符时,都需要访问到第M个位置,也就是p[i][M],要知道,这虽然只有一个字节的内存,却是你没有分配的,所以总归是"非法内存","非法访问".RE也不奇怪... Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator