1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Help!!segmentation fault

Discussion in 'C' started by drum123, Sep 10, 2011.

  1. drum123

    drum123 New Member

    Joined:
    Sep 10, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hello all,
    I am stuck and am unable to proceed anywhere on account of segmentation fault:( in the following code...can anyone temme what im doing wrong and how to rectify it.any help will be welcome :nice:

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    
    
    //Signum function
    int sig(float a)
    {
    if (a>=0)
    return 1 ;
    else return -1;
    }
    //Function to return absolute value
    /*double abs (double a)
    {
    if (a>=0)
    return a;
    else return -1*a;
    }
    */
    void main()
    
    {
         int *b,l=3000,k,i;
        double a[3000],**c;
     
        if (c==NULL)
                {
                  printf("error...""no memory left");
                  exit(0);
                }
        
    
    
    
    for(k=0;k<3000;k++)
    { a[k]=-k+0.6*k*0.6*k-0.36*k*k*k;
    }
    b=&l;
    
            for ( k=0;k<15;k++)
             
                {
                  c[k]=(double *)malloc(*b/15 * sizeof(double));
                          
             
              if (c[k]==NULL)
                    {
                          printf("error...""no memory left");
                          exit(0);
                    }
    
                }           
    
    
    
    double **denoising(double *arr,int *arrsize)
        {     
         int count=0,window[300],z_mean=0,stdev=0,start,stop,num=100,k,j;
         int *z_crossings=NULL;     
         double  thresh,temp[300],emean=0,**templ=NULL;
         double *energyframe=NULL;  
             
             
        // a block of memory is allocated to the pointer templ which is used to keep track of each template and contain its values
        templ=(double**)malloc(15*sizeof(double));
            
          
        if (templ==NULL)
                {
                  printf("error...""no memory left");
                  exit(0);
                }
        
            for ( k=0;k<15;k++)
             
                {
                  templ[k]=(double *)malloc(*arrsize/15 * sizeof(double));
                          
             
              if (templ[k]==NULL)
                    {
                          printf("error...""no memory left");
                          exit(0);
                    }
    
                }    
        
        /*Initialises a rectangular window of size 300//
        for (i=0;i<300;i++)
         { 
           window[i]=1;
         }
         */    
          start=0;
          stop=300;
             
         z_crossings= (int *)malloc((*arrsize-stop+1)*sizeof(int));
            
          if (z_crossings==NULL)
                {
                  printf("error...""no memory left");
                  exit(0);
                }
         
             energyframe=(double *)malloc((*arrsize-stop+1)*sizeof(double));
            
         if (energyframe==NULL)
                {
                  printf("error...""no memory left");
                  exit(0);
                }
    
    
    
    
    
    
          while(stop < *arrsize)
         {    
          energyframe[count]=0;
              z_crossings[count]=0;       
        
         
           for (j=start;j<stop;j++)
             {   
            
              //temp[j]=arr[j]*window[j-start];
                temp[j-start]=arr[j];
              //Computes energy in each frame//
              energyframe[count]+=temp[j-start]*temp[j-start];
                  //Coomputes zero crossingsin each frame//
                  z_crossings[count]+= 1/2*(abs(sig(temp[j-start+1])-sig(temp[j-start])));        
             }    
             
           start++;
               stop++;
               count++;   
         }
         
    for( j=0;j<count;j++)
     {
     emean +=energyframe[j]/count;
     //z_mean +=z_crossings[j]/count;//
     }
    /*for( j=0;j<count;j++)
     { 
      stdev+=((z_mean-z_crossings[j])^2)/count;
     }
    */
    
    //The mean of the energy in each frame is set as the threshold
    thresh=emean;
    //The threshold for zero crossings is set// 
    //num=z_mean + 3*sqrt(stdev);
    
    j=0;
    count=0;
    
    do
        { 
            if ((energyframe[j]>=thresh) && (z_crossings[j]<=num))
                { start=j ;
                    while((energyframe[j]>=thresh) && (z_crossings[j]<=num))
                    {
                        j++;
                    }
                 stop=j-1;
                 for (i=start;i<=stop;i++)
                    {
                     templ[count][i-start]=arr[i];//Records each signal frame(denoised) in a template//
                    }
                             count++ ;
                   }
            else j++;
            
        }while(j<*arrsize);
    
      free(z_crossings);
      free(energyframe);
      z_crossings=NULL;
       energyframe=NULL;
    
    return templ;
    free(templ);
    }
    c=denoising(a,b);
    int j=0;
    while(c!=NULL)
    { j=0;
      while(c[j]!=NULL)
      { 
       printf("%f",*c[j]);
        j++;
       }
    
    }
    free(c);
    }
     
    Last edited by a moderator: Sep 10, 2011
  2. marino.DV

    marino.DV Banned

    Joined:
    Sep 5, 2011
    Messages:
    79
    Likes Received:
    0
    Trophy Points:
    0
    well i can offer you a hello :P
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Moved to C-C++ forum
     
  4. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    1)The very first problem is what my compiler tells me when I try to compile this program
    2)
    Code:
    double a[3000],**c; 
        if (c==NULL)
        {
          printf("error...""no memory left");
          exit(0);
        }
    What are the intentions here? Firstly you are not initializing 'c' and then directly checking it for NULL ???

    3)
    c[k] means *(c+k). Which is value at address held at memory location 'c+k', but does the memory location 'c+k' belong to any variable in our program?
    This is a problem as we are trying to access the stack address which does not belong to any variable of our program.

    I could not go through the entire program as its quite messy. Can you explain what are you trying to achieve through this program?
     
  5. sura

    sura Banned

    Joined:
    Aug 4, 2011
    Messages:
    47
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    India,Tamil Nadu.
    just allocate the memory for c before checking c==null
     

Share This Page