Segmentation Fault

This is a discussion on Segmentation Fault within the C Programming forums, part of the General Programming Boards category; When I run this program I get segmentation fault.The code compiles and runs till the last line but when it ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    16

    Segmentation Fault

    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;
    }
    Last edited by kwzeet; 12-25-2010 at 04:06 AM. Reason: code tag missing

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You've probably overlaid something. Which free() is failing?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    If I free NN, I get "Segmentation Fault". If I free MM, I get "glibc detected()".If I don't free these two and free others(xint,wint), the code runs normal. I am not freeing any of these two more than once, because these two pointers are not being passed to any other function. They are used only in main() and then they are freed. I also checked the return value of malloc() and it's not NULL i.e. the memory is being allocated.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,337
    If a crash is occurring in free(), the usual cause is some preceding code (and, no, that doesn't just mean the corresponding malloc() call) has tromped an invalid area of memory, and happened to tromp on data structures used internally by malloc()/free/ etc.

    Given that a several sections of your code are working with arrays (or pointers that act on arrays), and the logic is not exactly straight-forward, I'd look there. Plenty of opportunity for mucking up an index, and walking off the end of an arrays.

    You also seem to have macros and variables representing the same thing (eg a macro named NUMWEIG - which isn't actually defined - and a variable named numWeig). If you have a mismatch between those, they may also cause some of your loops to fall off the end of arrays.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Thanks all. Actually this is a long code(~1000 lines) and I've declared all the macros in the beginning of the code.I posted only that part of the code where I was getting the error. But as Grumpy said that the error may be in some other part of the code,I tried to comment all the function calls except "getlikelHood" which generates data to populate MM matrix. I ran the code and again got the same error. I commented "getlikelHood", as well. And executed the code.I got the same error. Are you guys sure that there is no error in at least the code that I posted??

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,608
    Code:
    MM = (double *)malloc(numWeig*numWeig*sizeof(int));
    Why are you casting to "(double *)" but using sizeof(int)
    Normally the sizeof(type) would match what you want.
    I Question this; but, I am sick today so I could easily be wrong.

    Tim S

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    stahta01 is correct, the allocation sizes are way off.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Yes, you were right. I had not paid attention to that. I had allocated memory equal to the size of an int and was trying to free memory equal to size of a double. ooops!!.
    Thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2010, 10:55 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21