I’m having a problem figuring out how to initiate an array of struct pointers.
What i bascially want to do is loop through the array of struct pointers and for each pointer i want to do some work.
The code for the structure:
typedef double Align;
union header {
struct {
union header * p, *prev;
unsigned size;
} s;
Align x;
};
typedef union header Header;
and the code that i can’t get to work is following:
Header * freelist[NRQUICKLISTS]; /* Listan med */
void init() { /* call this once in the beginning */
int i;
for (i = 0; i < NRQUICKLISTS-1; i++) {
freelist[i]->s.p = freelist[i]; /* <-- this line cause Segmentation fault */
freelist[i]->s.size = 0;
}
freelist[i] = &base;
freelist[i]->s.p = freep = &base; /* cirkulär lista */
freelist[i]->s.size = 0;
}
You need to allocate memory for the pointers before you can use them:
Also, I suggest you do it the C++ way – which is using
std:vectorinstead of an array.Edit:
For C, use malloc: