Help!!segmentation fault

Newbie Member
10Sep2011,18:45   #1
drum123's Avatar
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

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 shabbir; 10Sep2011 at 19:10.. Reason: Code blocks
Banned
12Sep2011,02:51   #2
marino.DV's Avatar
well i can offer you a hello :P
Go4Expert Founder
12Sep2011,06:49   #3
shabbir's Avatar
Quote:
Originally Posted by marino.DV View Post
well i can offer you a hello :P
Moved to C-C++ forum
Ambitious contributor
13Sep2011,07:55   #4
poornaMoksha's Avatar
1)The very first problem is what my compiler tells me when I try to compile this program
Quote:
segfault.c:27: warning: ‘c’ is used uninitialized in this function
2)
Code: c
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)
Quote:
c[k]=(double *)malloc(*b/15 * sizeof(double));
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?
Banned
28Oct2011,23:51   #5
sura's Avatar
just allocate the memory for c before checking c==null