so currently we are learning C and we are begining to use pointers . This program should create a array of a size given then append the a value ( here 5 ) and print the array , then delete all the content and reprint the array . Everything works fine until the free(tab.data).
Here is the struct used .
typedef struct table_t {
double* data;
int size
} table_t;
and the code
#include <stdlib.h>
#include "TD1.h"
table_t table_new(int n) {
table_t t;
t.data = malloc(sizeof(double)*n);
}
void table_printf(table_t tab) {
int i ;
for(i=0;i<tab.size;i++){
printf("%lf \n",tab.data[i]);
}
}
int table_append(double val, table_t tab){
int i ;
for(i=0;i<tab.size;i++){
tab.data[i]= val ;
}
}
table_t table_delete(table_t tab)
{
free(tab.data) ; //Where the problem is using valgrind
}
int main()
{
table_t t ;
printf(" Enter the size of the array ");
scanf("%d ", &t.size );
table_new(t.size );
table_append(5,t);
table_printf(t);
table_delete(t);
table_printf(t);
return(0);
}