0

I got the program to read audio (.WAV)- courtesy: NAUFAL, CET
[CODE]#include <stdio.h>

include "wav.h"

main()
{
int16_t *samples = NULL;

wavread("track.wav", &samples);

printf("No. of channels: %d\n",     header->num_channels);
printf("Sample rate:     %d\n",     header->sample_rate);
printf("Bit rate:        %dkbps\n", header->byte_rate*8 / 1000);
printf("Bits per sample: %d\n\n",     header->bps);

printf("Sample 0:        %d\n", samples[0]);
printf("Sample 1:        %d\n", samples[1]);

// Modify the header values & samples before writing the new file
wavwrite("track2.wav", samples);

free(header);
free(samples);

}
[/CODE]
wav.h
[CODE]#include <inttypes.h>

include <stdlib.h>

include <sys/types.h>

include <sys/stat.h>

include <fcntl.h>

include <err.h>

typedef struct {
char chunk_id[4];
uint32_t chunk_size;
char format[4];

char     fmtchunk_id[4];
uint32_t fmtchunk_size;
uint16_t audio_format;
uint16_t num_channels;
uint32_t sample_rate;
uint32_t byte_rate;
uint16_t block_align;
uint16_t bps;

char     datachunk_id[4];
uint32_t datachunk_size;

}WavHeader;

WavHeader *header;

void wavread(char *file_name, int16_t **samples)
{
int fd;

if (!file_name)
    errx(1, "Filename not specified");

if ((fd = open(file_name, O_RDONLY)) < 1)
    errx(1, "Error opening file");

if (!header)
    header = (WavHeader*)malloc(sizeof(WavHeader));

if (read(fd, header, sizeof(WavHeader)) < sizeof(WavHeader))
    errx(1, "File broken: header");

if (strncmp(header->chunk_id, "RIFF", 4) ||
    strncmp(header->format, "WAVE", 4))
    errx(1, "Not a wav file");

if (header->audio_format != 1)
    errx(1, "Only PCM encoding supported");

if (*samples) free(*samples);
*samples = (int16_t*)malloc(header->datachunk_size);
if (!*samples)
    errx(1, "Error allocating memory");

if (read(fd, *samples, header->datachunk_size) < header->datachunk_size)
    errx(1, "File broken: samples");

close(fd);

}

void wavwrite(char file_name, int16_t samples)
{
int fd;

if (!file_name)
    errx(1, "Filename not specified");

if (!samples)
    errx(1, "Samples buffer not specified");

if ((fd = creat(file_name, 0666)) < 1)
    errx(1, "Error creating file");

if (write(fd, header, sizeof(WavHeader)) < sizeof(WavHeader))
    errx(1, "Error writing header");

if (write(fd, samples, header->datachunk_size) < header->datachunk_size)
    errx(1, "Error ...
0
  1. I am using a function 'free1' to free memory of 2D pointer 'a'. Is there any memory leak. ( I am still learning valgrind not yet ready to work on it)
    [CODE]#include<stdio.h>

    include<math.h>

    include<stdlib.h>

define DIM_1 3/don't use magic numbers, instead use a descriptive name/

define DIM_2 3/don't use magic numbers, instead use a descriptive name/

double memalloc(int M,int N);
void free1(double
a,int M);

int main()/main returns an int/
{
int i,j;
double **a;

a = memalloc(DIM_1, DIM_2);

for(i = 0; i < DIM_1; i++)
{
    printf("\n");

    for(j = 0; j < DIM_2; j++)
    {
        a[i][j]=i+j;
        printf("%f\t",a[i][j]);      
    }
}

fputc('\n', stdout);
free1(a,DIM_1);
getchar();
return 0;

}

double memalloc(int M,int N)
{
int i;
double
y;

y = malloc(M* sizeof(double *));

if(y == NULL)
{
    printf("out of memory\n");
    return 0;
}

for(i = 0; i < M; i++)
{
    y[i] = malloc(N * sizeof(double));
    if(y[i] == NULL)
    {
        printf("out of memory\n");
        return 0;
    }
} 
return y;

}
void free1(double **a,int M)
{
int i;
for(i = 0; i < M; i++)
{
free(a[i]);
}
free(a);
}
[/CODE]

  1. which is a good programming practice?
    a. free the memory of a varible when that variable is not used in the lines below in main()?
    b. free all the variables at the end of main() (I think this is useless, since by the end of main(), we no longer need any memory)
0

1.[QUOTE]Any help? I told you what's wrong with your code. Correct the problems and your code will work. [/QUOTE]
sorry, I didnt see the second page
[CODE]#include<stdio.h>

include<math.h>

include<stdlib.h>

double memalloc(int M,int N);
main()
{
int i,j;
double
a;
a=memalloc(2,2);
printf("a\n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
a[i][j]=i+j;
printf("%f\t",a[i][j]);
}
}
//free(a);
}
//FUNCTION-memalloc
double memalloc(int M,int N)
{
int i;
double
y;
y = malloc(M sizeof(double ));
if(y == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < M; i++)
{
y[i] = malloc(N * sizeof(double));
if(y[i] == NULL)
{
printf("out of memory\n");
return 0;
}
free(y[i]);
}
//free(y);
return y;
}[/CODE]
2.The above program works good. But why is the line 43 (now shown as comment)[CODE]free(y)[/CODE] not workig, where [CODE]free(a)[/CODE] works good? (Both are 2 dimensional pointers, and I dont see any difference.)
3.The above program has no memory leakage and no more freeing of memory is needed in the above program. right?

  1. One fundamental doubt. I am freeing the allocated memory inside the function itself, then which memory location is being accessed by the main program for a?
    Thanks...
0

[QUOTE]Your program creates a memory leak by free'ing a before free'ing the other allocated memory areas.
[/QUOTE]
I hope, the above quote means I should free y before freeing a. then segmentation fault comes.
[CODE]#include<stdio.h>

include<math.h>

include<stdlib.h>

double memalloc(int M,int N);
main()
{
int i,j;
double
a;
a=memalloc(2,2);
printf("a\n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
a[i][j]=i+j;
printf("%f\t",a[i][j]);
}
}
free(a);
}
//FUNCTION-memalloc
double memalloc(int M,int N)
{
int i;
double
y;
y = malloc(M sizeof(double ));
if(y == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < M; i++)
{
y[i] = malloc(N * sizeof(double));
if(y[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
free(y);
return y;
}
[/CODE]

0

@gerard4143
thats rt. Since i am using the allocated memory of y for a, i should free a and not y. then the program is [CODE]#include<stdio.h>

include<math.h>

include<stdlib.h>

double memalloc(int M,int N);
main()
{
int i,j;
double
a;
a=memalloc(2,2);
printf("a\n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
a[i][j]=i+j;
printf("%f\t",a[i][j]);
}
}
free a;
}
//FUNCTION-memalloc
double memalloc(int M,int N)
{
int i;
double
y;
y = malloc(M sizeof(double ));
if(y == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < M; i++)
{
y[i] = malloc(N * sizeof(double));
if(y[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
return y;
}
[/CODE]
still same error comes. [QUOTE]test.c:20: error: expected ‘;’ before ‘a’[/QUOTE]

0

1.[ICODE]#include<stdio.h>

include<math.h>

include<stdlib.h>

double memalloc(int M,int N);
main()
{
int i,j;
double
a;
a=memalloc(2,2);
printf("a\n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
a[i][j]=i+j;
printf("%f\t",a[i][j]);
}
}
}
//FUNCTION-memalloc
double memalloc(int M,int N)
{
int i;
double
y;
y = malloc(M sizeof(double ));
if(y == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < M; i++)
{
y[i] = malloc(N * sizeof(double));
if(y[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
free y;
return y;
}
[/ICODE]
the line [CODE]free y[/CODE] cause problems. should i use it after [CODE]return y[/CODE]

  1. Is there any way to check whether the memory is freed?
0

i allocate memory for b_edge in function bark_edge and give value to only b_edge[1][1] and try to return b_edge from function. segmentation fault comes? can you please check why?
[CODE]//function bark_edge+main program

include<stdio.h>

include<math.h>

include<stdlib.h>

double bark_edge();
main()
{
double
b_edge;
b_edge=bark_edge();
printf("%f",b_edge[1][1]);
}
//FUNCTION-bark_edge
double bark_edge()
{
int i=0;
double
b_edge;
//memory allocation for b_edge
b_edge = (double*)malloc(10 sizeof(double ));
if(b_edge== NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i <1024 ; i++)
{
b_edge[i] = (double
)malloc(10 * sizeof(double));
if(b_edge[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
b_edge[1][1]=2;
return b_edge;
}
[/CODE]

0

above is the c implementation of the matlab file below. is there any matlab to c converter... just to know..
[CODE]
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.054center(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';

[/CODE]

0

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...
[ICODE]
//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(nrowssizeof(double));
if(center==NULL)
{
printf("out of memory\n");
return 0;
}
band_edge=(double)malloc(nrowssizeof(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.054center[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;
}[/ICODE]