The following code is giving me a seg fault. gdb says its coming from the line memos[j][k] -> cost = -1;
, but I can't tell exactly what is wrong. I'm guessing something with how I am allocating memory and that the array index is mistakenly out of bounds?
memo_array_t mk_empty_memo_array(int l) {
int n = pow(2, l+2);
memo_t **memos = (memo_t **) malloc(n*sizeof(struct memo_s));
for(int i = 0; i < pow(2, l+2); i++) {
memos[i] = (memo_t *) malloc(n*sizeof(struct memo_s *));
}
for (int j = 0; j < n; j++) {
for (int k = 0; k < pow(2, l+2); k++) {
memos[j][k] -> cost = -1;
memos[j][k] -> color = -1;
memos[j][k] -> split = -1;
memos[j][k] -> box = NULL;
memos[j][k] -> split_value = -1;
}
}
memo_array_t memo_array = (memo_array_t) malloc(sizeof(struct memo_array_s));
memo_array -> dim = n;
memo_array -> memos = memos;
return memo_array;
}
In case you want to see the struct typedefs:
typedef struct memo_s {
int cost;
int color;
int split;
double split_value;
box_t box;
} *memo_t;
typedef struct memo_array_s {
int dim;
memo_t **memos;
} *memo_array_t;