c - malloc inside function causing SIGSEV -
i trying write generic queue implementation in c. initialization part, have:
struct queue{ int head; int tail; int max_size; int elem_size; void **elements; }; void queue_init(struct queue** q, int max_size, int elem_size){ *q = malloc(sizeof(struct queue)); (*q)->head = (*q)->tail =0; (*q)->max_size = max_size; (*q)->elem_size = elem_size; (*q)->elements = malloc(max_size * elem_size); }
to test implementation, wrote following:
void klein_test(void){ struct queue** qp; queue_init(qp, 1, 1); return; } int main(){ klein_test(); }
while trying run such test, got sigsev
. inspecting execution gdb
, see error signal being generated @ queue_init
's first call malloc
. cause of such behaviour?
at least write
struct queue* qp; queue_init(&qp, 1, 1);
also not clear why there declaration
void **elements;
instead of just
void *elements;
Comments
Post a Comment