本文介绍了为什么这段代码显示分段错误(核心转储)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! #include < iostream > 使用 命名空间标准; class wave { public : void initialdata( double **, int , int ); void solveFD( double **, double *, double *, double , double , double , int , int ); }; void wave :: initialdata( double ** u, int nx, int nt) { int 我,j; for (j = 1 ; j< nt; j ++) 的类=code-keyword>(i = 1 ; i< nx; i ++) { if (i< = 20 ) {u [i] [j] = 2 。 0 ; } else {u [i] [j] = 1 。 0 ;} } } // void wave :: solveFD( double * * u, double * x, double * t, double c, double dt, double dx, int nx, int nt) { int i,焦耳; for (j = 1 ; j< nt; j ++) 的类=code-keyword>(i = 1 ; i< nx; i ++) {x [i] =(i- 1 )* dx; t [j] =(j- 1 )* dt; u [i] [j + 1] = u [i] [j] - (c * dt / dx)*(u [i + 1] [j] -u [i] [j]); } } // int main() { int i,j; int numx = 101 ,numt = 101 ; double cvalue = 1 。 0 ; double ** umatrix,* xmatrix,* tmatrix,dtval,dxval; dtval = 1 .0 /(numt- 1 ); dxval = 1 .0 /(numx- 1 ); umatrix = new double * [numx + 1]; xmatrix = new double [numx + 1]; tmatrix = new double [numt + 1]; for (i = 1 ; i< numt; i ++) { umatrix [i] = new double [numt + 1]; } wave wave1; wave1.initialdata(umatrix,numx,numt); wave1.solveFD(umatrix,xmatrix,tmatrix,cvalue,dtval,dxval,numx,numt); for (j = 1 ; j< numt; j ++) for (i = 1 ; i< numx; i ++) { cout<< ; umatrix [i] [j]; } for (i = 1 ; i< numt; i ++) { delete umatrix [i]; } delete umatrix,xmatrix,tmatrix; return 0 ; } 解决方案 作为 Ujesh Mohanan ,注意到,我以前的洗礼是错误的,因为你实际分配了矩阵。但是你忘记了最后一项: (i = 1 ; i< numt; i ++) { umatrix [i] = new double [numt + 1]; } 应该是 for (i = 1 ; i< = numt; i ++) { umatrix [i] = new double [numt + 1]; } 再一次,我强烈建议你使用 std :: vector< double> 而不是弄乱原始指针。 顺便说一句: 引用:删除umatrix,xmatrix,tmatrix; 错误。 #include<iostream>using namespace std;class wave{public: void initialdata(double **,int ,int); void solveFD(double **,double *,double *,double ,double ,double,int,int );};void wave::initialdata(double **u,int nx,int nt){ int i,j; for (j=1;j<nt;j++) for (i=1;i<nx;i++) { if(i<=20) { u[i][j]=2.0; } else {u[i][j]=1.0;} }}//void wave::solveFD(double **u,double *x,double *t,double c,double dt,double dx,int nx,int nt){ int i,j; for(j=1;j<nt;j++) for (i=1;i<nx;i++) { x[i]=(i-1)*dx; t[j]=(j-1)*dt; u[i][j+1]=u[i][j]-(c*dt/dx)*(u[i+1][j]-u[i][j]); }}//int main(){ int i,j; int numx=101,numt=101; double cvalue=1.0; double **umatrix,*xmatrix,*tmatrix,dtval,dxval; dtval=1.0/(numt-1); dxval=1.0/(numx-1); umatrix=new double *[numx+1]; xmatrix=new double [numx+1]; tmatrix=new double [numt+1]; for (i=1;i<numt;i++) { umatrix[i]=new double [numt+1]; } wave wave1; wave1.initialdata(umatrix,numx,numt); wave1.solveFD(umatrix,xmatrix,tmatrix,cvalue,dtval,dxval,numx,numt); for (j=1;j<numt;j++) for (i=1;i<numx;i++) { cout<<umatrix[i][j]; } for (i=1;i<numt;i++) { delete umatrix[i]; } delete umatrix,xmatrix,tmatrix; return 0;} 解决方案 As Ujesh Mohanan, noted, my previous soulution was wrong, since you actually allocated the matrix. However you forgot the last item:for (i=1;i<numt;i++) { umatrix[i]=new double [numt+1]; }Should be insteadfor (i=1;i<=numt;i++) { umatrix[i]=new double [numt+1]; }Again, I strongly suggest you using std::vector<double> instead with messing with raw pointers.By the way:Quote:delete umatrix,xmatrix,tmatrix;is wrong. 这篇关于为什么这段代码显示分段错误(核心转储)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 20:45