segmentation fault comes in the program? please help? is there any way i can check in which part of the program is the mistake? Thanks daniweb and members...

//function bark_edge+main program
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double **bark_edge(int f_size,int samp_freq);
double **transpose(double **x,int M,int N);
main()
{
  int f_size=1024;
  float samp_freq=44100,fcmax=samp_freq/2;
  double **b_edge;

  b_edge=bark_edge(f_size,samp_freq);
}
//FUNCTION-bark_edge
double **bark_edge(int f_size,int samp_freq)
{
  int i=0,j=1,a=1,count=0,nrows=1024;//nrows taken as 1024 arbitrarily  
  float fcmax=samp_freq/2,freq_reso=fcmax/f_size;
  double *band_edge,*center,**b_edge1,**b_edge;
  //memory allocation-center,band_edge
  center=(double*)malloc(nrows*sizeof(double));
  if(center==NULL)
    {
      printf("out of memory\n");
      return 0;
    }
  band_edge=(double*)malloc(nrows*sizeof(double));
  if(band_edge==NULL)
    {
      printf("out of memory\n");
      return 0;
  }
  //memory allocation-b_edge1
  b_edge1= (double**)malloc(nrows* sizeof(double *));
  if(b_edge1 == NULL)
    {
      printf("out of memory\n");
      return 0;
    }
  for(i = 0; i <nrows; i++)
    {
      b_edge1[i] = (double*)malloc(nrows* sizeof(double));
      if(b_edge1[i] == NULL)
      {
        printf("out of memory\n");
        return 0;
      }
    }  
  while(center[i]<=fcmax)
    {
      i=i+1;
      band_edge[i-1]=center[i-1]+a*(12.35+0.054*center[i-1]);
      center[i]=(band_edge[i-1]+12.35)/0.946;
      b_edge1[i-1][0]=band_edge[i-1]/freq_reso;
      count++;
    }
  for(i=2;i<count;i+2)
    {
      b_edge[j][0]=b_edge1[i][0];
      j++;
    }
  b_edge[0][0]=1;
  b_edge[count][0]=f_size;
  b_edge=transpose(b_edge,count/2,1);
  return b_edge;
}
//FUNCTION_transpose
double **transpose(double **x,int M,int N)
{
  int nrows=N,ncolumns=M,i,j;
  double **y;
  //memory allocation for y,to store transpose
  y = (double**)malloc(nrows * sizeof(double *));
  if(y == NULL)
    {
      printf("out of memory\n");
      return 0;
    }
  for(i = 0; i < nrows; i++)
    {
      y[i] = (double*)malloc(ncolumns * sizeof(double));
      if(y[i] == NULL)
    {
      printf("out of memory\n");
      return 0;
    }      
    }
  //taking transpose
  for(i=0;i<N;i++)
    for(j=0;j<M;j++)
      {
    y[i][j]=x[j][i];
      }
  return y;
}

Recommended Answers

All 7 Replies

above is the c implementation of the matlab file below. is there any matlab to c converter... just to know..

function b_edge = bark_edge(f_size,samp_freq)


fcmax = samp_freq/2;

center(1) = 24.7/1.892;

band_edge(1) = 0;

freq_reso = fcmax/f_size;

i = 1;

a = 1;

while(center(i)<=fcmax)
    
  i = i+1;
    
  band_edge(i-1) = center(i-1) + a*(12.35+0.054*center(i-1));
    
  center(i) = (band_edge(i-1) + 12.35)/0.946;
   
end

b_edge = round(band_edge/freq_reso);

b_edge = b_edge(3:2:end-1);

b_edge = [1 b_edge f_size];

b_edge = b_edge';

valgrind is an invaluable tool (if you are on a linux platform). I have not investigated this output, but running your program through valgrind produces

==12171== Invalid read of size 8
==12171==    at 0x804858E: bark_edge (t.c:50)
==12171==    by 0x8048418: main (t.c:13)
==12171==  Address 0x4024028 is 0 bytes after a block of size 8,192 alloc'd
==12171==    at 0x4004405: malloc (vg_replace_malloc.c:149)
==12171==    by 0x8048480: bark_edge (t.c:22)
==12171==    by 0x8048418: main (t.c:13)
==12171==
==12171== Invalid read of size 8
==12171==    at 0x80485CD: bark_edge (t.c:53)
==12171==    by 0x8048418: main (t.c:13)
==12171==  Address 0x4024028 is 0 bytes after a block of size 8,192 alloc'd
==12171==    at 0x4004405: malloc (vg_replace_malloc.c:149)
==12171==    by 0x8048480: bark_edge (t.c:22)
==12171==    by 0x8048418: main (t.c:13)
==12171==
==12171== Invalid read of size 8
==12171==    at 0x80485E1: bark_edge (t.c:53)
==12171==    by 0x8048418: main (t.c:13)
==12171==  Address 0x4024028 is 0 bytes after a block of size 8,192 alloc'd
==12171==    at 0x4004405: malloc (vg_replace_malloc.c:149)
==12171==    by 0x8048480: bark_edge (t.c:22)
==12171==    by 0x8048418: main (t.c:13)
==12171==
==12171== Invalid write of size 8
==12171==    at 0x80485E5: bark_edge (t.c:53)
==12171==    by 0x8048418: main (t.c:13)
==12171==  Address 0x4026058 is 0 bytes after a block of size 8,192 alloc'd
==12171==    at 0x4004405: malloc (vg_replace_malloc.c:149)
==12171==    by 0x80484B7: bark_edge (t.c:28)
==12171==    by 0x8048418: main (t.c:13)
==12171==
==12171== Invalid read of size 8
==12171==    at 0x8048600: bark_edge (t.c:54)
==12171==    by 0x8048418: main (t.c:13)
==12171==  Address 0x4026058 is 0 bytes after a block of size 8,192 alloc'd
==12171==    at 0x4004405: malloc (vg_replace_malloc.c:149)
==12171==    by 0x80484B7: bark_edge (t.c:28)
==12171==    by 0x8048418: main (t.c:13)
<snip>

this code is used for returning a one dimensional array containing bark edge frequencies. I use this for an AUDIO CODEC.

@L7sqr. I didnt understand anything from that, can i know at which line is the error?

in the first c program, i am using the same memory location of b_edge1 for storing the alternative elements of it. that is i just declare b_edge

int **b_edge

(and store the alternate elements in it ) and no separate memory allocation is done for b_edge. will it be a problem. (i no longer need b_edge1)

==12171== Invalid read of size 8
==12171==    at 0x80485CD: bark_edge (t.c:53)
==12171==    by 0x8048418: main (t.c:13)
==12171==  Address 0x4024028 is 0 bytes after a block of size 8,192 alloc'd
==12171==    at 0x4004405: malloc (vg_replace_malloc.c:149)
==12171==    by 0x8048480: bark_edge (t.c:22)
==12171==    by 0x8048418: main (t.c:13)

Problem: Invalid read of size 8
Location: t.c, line 53

I copied your program so it should be on line 53 of your source file.

band_edge[i-1]=center[i-1]+a*(12.35+0.054*center[i-1]);

The address was allocated on t.c, line 22

center=(double*)malloc(nrows*sizeof(double));

The problem (at least this one) seems to stem from the following construct in your code:

int i = 0;
for (i = 0; i < nrows; ++i) {
   ...
}
while (center[i] <= cfmax) { // THIS IS A PROBLEM
   ...
}

What is the value of i when you index center in that statement? And you continue to increment it along the way...

I found a problem with i. I have to reinitialize it to zero before the loop

while (center[i] <= cfmax) { // THIS IS A PROBLEM
   ...
}

.
still more mistakes. i will try once more. thanks...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.