fix length difference

now making a queue with size n will actually let you store n elements in
it instead of n-1
This commit is contained in:
jacekpoz 2024-03-10 14:13:04 +01:00
parent df960e552c
commit 761c019407
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
2 changed files with 2 additions and 2 deletions

View file

@ -7,7 +7,7 @@
int main(void) { int main(void) {
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
Queue *q = queue_new(51, sizeof(long)); Queue *q = queue_new(50, sizeof(long));
for (size_t i = 0; i < 50; ++i) { for (size_t i = 0; i < 50; ++i) {
long val = rand() % 1000; long val = rand() % 1000;

View file

@ -17,7 +17,7 @@ Queue *queue_new(size_t length, size_t element_size) {
ret->front = 0; ret->front = 0;
ret->length = length; ret->length = length;
ret->element_size = element_size; ret->element_size = element_size;
ret->data = malloc(length * sizeof(void*)); ret->data = malloc((length + 1) * sizeof(void*));
return ret; return ret;
} }