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 ...

thank you.

  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)

ok. thats the answer i needed. thanks

Ok

[QUOTE] for(i = 0; i < DIM_1; i++)
{
free(a[i]);
}[/QUOTE]1.Is this step must? (since you havnt shown this in first post)
2.After freeing a in that way, why is [CODE]free(a)[/CODE] needed?

Thanks

1.It means i dont have to return the matrices but just define them in the main program and change the values of the matrices inside the function. ritht?

  1. Will it be better if I declare sum and product as global variables (before main()) and find them inside function without returning them?

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...

any help?

i want to write a function to which i need to pass two matrices and get the sum and product of them as output. but how can i return more than one matrix from a function? (i want to pass and return using pointers)

[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]

@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]

tried that also. still error persists

but i am getting the error
[CODE]test.c:41: error: expected ‘;’ before ‘y’[/CODE]
where 41st line is [CODE]free y[/CODE]

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?

thanks... i was converting matlab to c. so didnt notice...

by a^b i mean 'a raised to b' (bth power of a). not bitwise operations.

[CODE]double a,b;
printf("%f",a^b);[/CODE]
error occurs as shown in heading.
any alternative method?

is the position of 'free a' in pgm correct?
[CODE]function(variables)
{
a=malloc(...);
....
return a;
free a; //is this the correct position to free a?
}[/CODE]

  1. any problem if i dynamically allocate memory and use only a small part of it?
  2. can i free the dynamic memory after each time the function is executed?

k it worked...
admins plz delete this thread. sry for inconvenience

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]

I found a problem with i. I have to reinitialize it to zero before the loop
[CODE]while (center[i] <= cfmax) { // THIS IS A PROBLEM
...
}[/CODE].
still more mistakes. i will try once more. thanks...

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 [CODE]int **b_edge[/CODE] (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)

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

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

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]

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]

thank you... i think i have my answer...