When I run this program I get segmentation fault.The code compiles and runs till the last line but when it encounters the "free()" commands in the last lines in main, it gives segmentation fault. when i comment the free command, the code runs normal.

Code:int main() { double *weig; tm tyme; mdl model; int numWeig = 2,ctr=0; int i,j,ii,k,npts; double *MM, *NN,*MMtmp,*MMtmp2; double *xint1,*xint2,*wint1,*wint2; double value; double temp1[DIMENSION]; double temp2[DIMENSION]; double Pk[NUMWEIG]; double Lk[NUMWEIG]; double mutmp[NUMWEIG][DIMENSION]; double numOfPoints = 1; struct timeval tm; time_t ts ,te; double tdiff; double sig1[2][3][3]; double sig2[2][3][3]; model.fn = 3; tyme.dt = 0.01; npts = POINTS; ts = 0;te = 0; MM = (double *)malloc(numWeig*numWeig*sizeof(int)); NN = (double *)malloc(numWeig*numWeig*sizeof(int)); for(i = 0;i < NUMWEIG;i++) { for(j = 0;j<NUMWEIG;j++) { *(MM+i*NUMWEIG+j) = 0; } } MMtmp = MM; MMtmp2 = MM; double sigtmp[2][3][3]; double sig[2][3][3]={{{38.4276,50.4741,0.7706},{50.4741,66.7545,0.6300},{0.7706,0.6300,0.7731}}, {{36.1442,46.9353,-0.6776},{46.9353,61.4765,-0.3843},{-0.6776,-0.3843,0.9562}}}; double mu[2][3] = {{2.5898,3.3019,21.4641}, {-2.5898,-3.3019,21.4641}}; for(i = 0;i<model.fn;i++) { numOfPoints = numOfPoints*npts; } xint1 = (double *)malloc(numOfPoints*model.fn*sizeof(double)); xint2 = (double *)malloc(numOfPoints*model.fn*sizeof(double)); wint1 = (double *)malloc(numOfPoints*sizeof(double)); wint2 = (double *)malloc(numOfPoints*sizeof(double)); ts = clock(); for(i = 0,ii = i+1;i<numWeig,ii<numWeig;i++,ii++) { for(j = 0;j < model.fn;j++) { for(k = 0;k < model.fn; k++) { sigtmp[i][j][k] = sig[i][j][k]+sig[ii][j][k]; mutmp[i][k] = mu[i][k] - mu[ii][k]; } } } for(i = 0;i<numWeig;i++) { for(j = 0;j < model.fn;j++) { for(k = 0;k < model.fn; k++) { sig1[i][j][k] = sig[i][j][k]; sig2[i][j][k] = sig[i][j][k]; } } } for(i = 0;i < numWeig;i++) { for(j = i+1;j < numWeig;j++) { *(MMtmp+i*numWeig+j) = getlikelHood(mutmp[i],&sigtmp[i][0][0]); } if(chckdgnl(&sig1[i][0][0],model.fn)==0) value=0; else value=deter(&sig1[i][0][0],model.fn); *(MM+i*NUMWEIG+i) = (double)(1/sqrt(16*pi*pi*pi*value)); } for(i = 0;i<numWeig;i++) { for(j = 0;j<numWeig;j++) { *(MM+i*numWeig+j) = (double)(*(MM+i*numWeig+j)+*(MMtmp+i*numWeig+j)+*(MMtmp+j*numWeig+i))/(tyme.dt*tyme.dt); } } GenerateQuadPoints(&sig2[0][0][0],mu[0],npts,model.fn,xint1,wint1); GenerateQuadPoints(&sig2[1][0][0],mu[1],npts,model.fn,xint2,wint2); for(ctr = 0; ctr<numOfPoints; ctr++) { for(j=0;j<DIMENSION;j++) { temp1[j] = *((xint1)+j+ctr*DIMENSION); temp2[j] = *((xint2)+j+ctr*DIMENSION); } ComputeErrCoeff_QP(temp1,temp2,*(wint1+ctr),*(wint2+ctr),mu,&sig[0][0][0],DIMENSION,tyme.dt,Pk,Lk); printf("\nIteration Number = %d\n",ctr); for(i = 0;i<numWeig;i++) { for(j = 0;j<numWeig;j++) { *(NN+i*numWeig+j)= *(NN+i*numWeig+j)+Pk[i]*Lk[j]; } } } te = clock(); tdiff = te-ts; printf("\ntime elapsed = %f",(double)(te-ts)); for(i = 0;i<2;i++) { printf("\n"); for(j = 0;j<2;j++) { printf("\t%f",*(NN+i*2+j)); } } printf("\n"); free(NN); free(MM); free(xint1); free(xint2); free(wint1); free(wint2); return 0; }