/* Queue structure which holds all necessary data */
typedef struct queue {
int head;
int tail;
void** vals;
int len;
int count;
} queue_t;
/* creates a new queue with a given size */
queue_t* create_queue(int capacity){
queue_t* q = NULL;
q->len = capacity;
q->vals = malloc(sizeof(void*)*q->len);
q->head = 0;
q->tail = 0;
q->count = 0;
return q;
}
I can't see where could be problem. I tried to run my code with valgrind and it says:
==14818== Memcheck, a memory error detector
==14818== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==14818== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==14818== Command: ./main
==14818==
==14818== Invalid write of size 4
==14818== at 0x10918A: create_queue (in /mnt/c/Users/jakub/projects/bab36prga-hw6/main)
==14818== by 0x10946F: main (in /mnt/c/Users/jakub/projects/bab36prga-hw6/main)
==14818== Address 0x10 is not stack'd, malloc'd or (recently) free'd
==14818==
==14818==
==14818== Process terminating with default action of signal 11 (SIGSEGV)
==14818== Access not within mapped region at address 0x10
==14818== at 0x10918A: create_queue (in /mnt/c/Users/jakub/projects/bab36prga-hw6/main)
==14818== by 0x10946F: main (in /mnt/c/Users/jakub/projects/bab36prga-hw6/main)
==14818== If you believe this happened as a result of a stack
==14818== overflow in your program's main thread (unlikely but
==14818== possible), you can try to increase the size of the
==14818== main thread stack using the --main-stacksize= flag.
==14818== The main thread stack size used in this run was 8388608.
==14818==
==14818== HEAP SUMMARY:
==14818== in use at exit: 0 bytes in 0 blocks
==14818== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==14818==
==14818== All heap blocks were freed -- no leaks are possible
==14818==
==14818== For lists of detected and suppressed errors, rerun with: -s
==14818== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault
I am not really sure what that means, because the only thing that could be wrong is malloc so... am I not allowed to malloc space for void** array?
I am trying to create circular queue with dynamic reallocation (so you can push in unlimited amount of elements) but I am stuck at basic initialization. I am new to valgring and I would appriciate if somebody halped me to interpret where is the problem.