I do not understand why I can use malloc with struct var **tableauVariables, but I receve an error with struct zone **tableauZonesMemoireLibres.
When debuging I receve :
At C:\Users\Onel\Desktop\test\test.c:33 Program received signal SIGSEGV, Segmentation fault.
My code :
#include <stdlib.h>
void tabInit(), tabDelete();
struct zone {
int pos;
int taille;
};
struct var {
char id[10];
struct zone tableau;
};
struct var **tableauVariables;
struct zone **tableauZonesMemoireLibres;
int tailleMemoire= 10;
int main () {
tabInit();
tabDelete();
return 0;
}
void tabInit() {
tableauVariables = (struct var**) malloc(sizeof(struct var) * tailleMemoire);
tableauVariables[0] = NULL;
tableauZonesMemoireLibres = (struct zone**) malloc(sizeof(struct zone) * tailleMemoire);
tableauZonesMemoireLibres[0]->pos = 0; //Line 33 segmentation fault
tableauZonesMemoireLibres[0]->taille = tailleMemoire;
tableauZonesMemoireLibres[1] = NULL;
return;
}
void tabDelete() {
int i;
for(i=0; tableauZonesMemoireLibres[i] != NULL; i++)
free(tableauZonesMemoireLibres[i]);
free(tableauZonesMemoireLibres);
for(i=0; tableauVariables[i] != NULL; i++)
free(tableauVariables[i]);
free(tableauVariables);
}
tableauVariables = malloc(sizeof(struct var *) * tailleMemoire);or simpler and more robust:tableauVariables = malloc(tailleMemoire * sizeof *tableauVariables );tableauZonesMemoireLibres: similar error.