I am trying to dynamically initializing a queue. Here is my function.
typedef struct{
int size;
int max_size;
short * eles;
} queue;
void dump_queue(queue *q)
{
//print a bunch of information
}
void init_queue(queue *q, int max_size)
{
q = (queue)malloc(sizeof(queue));
q->size = 0;
q->max_size = max_size;
q->eles = (short *)malloc(max_size * sizeof(short));
int i;
for(i = 0; i < max_size; i++)
q->eles[i] = -1;
dump_queue(q);
}
task_queue is a global variable. The structure of the routine is like:(not exact code)
//globally defined here but not initialized
queue * task_queue;
void init_scheduler()
{
init_queue(task_queue, 32);
dump_queue(task_queue);
//other staff
}
note that there are two dump_queue, one is init_queue(), the other is after init_queue. Since task_queue is malloced, I expect that two calls of dump_queue should give a same result. But the second one actually reported a SIG_FAULT.
After I checked, in the second call of dump_queue, task_queue is actually NULL. Why is that?