Duozhasht
Duozhasht

Reputation: 9

C Memory Management -> Queue

Okay so i have this Queue implementation that works, but i'm having some memory leak that doesn't let a if else operations runs after that.

File queue_arr.h

#define MaxQ 100

typedef struct {
    int array[MaxQ];
    int front;
    int back;
    int size;
}*Queue;


Queue init();
int enqueue(Queue q, int v);
int dequeue(Queue q, int *v);
int front(Queue q, int *v);
int isEmpty(Queue q);

File queue_arr.c

#include "queue_arr.h"
#include <stdlib.h>
#include <stdio.h>


Queue init() {
    Queue q = (Queue)malloc(sizeof(Queue));
    q->front = 0;
    q->back = 0;
    q->size = 0;
    return q;
}

int enqueue(Queue q, int v) {
    if(q->size==MaxQ)
        return 0;
    q->array[q->back] = v;
    q->back++;
    if(q->back == MaxQ)
        q->back = 0;
    q->size++;
    return 1;

}

int dequeue(Queue q, int *v) {
    if(q->size == 0)
        return 0;
    *v = q->array[q->front];
    q->front++;
    if(q->front == MaxQ)
        q->front =0;
    q->size--;
    return 1;   
}

int front(Queue q, int *v) {
    if(q->size==0)
        return 0;
    *v = q->array[q->front];
    return 1;   
}

int isEmpty(Queue q) {
    if(q->size == 0)
        return 1;
    return 0;
}

int main(){
    Queue teste = init();
    int *aux;
    *aux = -1;
    printf("Value : %d\n",*aux );

    enqueue(teste,5);
    enqueue(teste,10);
    enqueue(teste,15);
    front(teste,aux);

    printf("Next costumer: %d\n",*aux );

    dequeue(teste,aux);

    printf("Costumer %d left queue\n",*aux );

    dequeue(teste,aux);

    printf("Costumer %d left queue\n",*aux );

    dequeue(teste,aux);

    printf("Costume %d left queue\n",*aux );

    int random = 10;
    if(random == 10)
        printf("asdasdasd\n");


}

The last if and else statement doest not work, i noticed cause i was trying to do a isEmpty if clause, and keep leading me to segmentation fault.

Without the last three lines, the code compiles and runs, with no errors. But with that i just keep getting a segmentation fault.

Does anybody knows the problem ?

Upvotes: 0

Views: 380

Answers (3)

Joker_vD
Joker_vD

Reputation: 3765

Queue q = (Queue)malloc(sizeof(Queue)); 

Congratulations, sizeof(Queue) is 4 (or 8) bytes since it's a pointer.

Upvotes: 1

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

Your segmentaion fault because of this

int *aux;

*aux = -1;

you should

aux = malloc(sizeof(int));

before

*aux = -1;

but I don't think that is what you want, instead you should just do it

int aux = -1;

and when calling dequeue

dequeue(teste, &aux);

Also I would recomend you fix these other issues

  1. You don't need to cast malloc
  2. You should check that malloc didn't return NULL otherwise you will dereference a NULL pointer which is undefined behavior.
  3. Also, your malloc call is passing the wrong size. A possible fix is to use

    Queue q = malloc(sizeof(*q));
    

    But I would actually recommend to avoid hiding the fact that variables are pointers completely.

  4. You do have a memory leak but that is not a cause for a segmentaion fault, you should just call free(teste) at the end of main().
  5. Your main() doesn't return you should, also add return 0; at the end of main() after free()

I fixed your code, hope this helps

#include <stdlib.h>
#include <stdio.h>

#define MaxQ 100

typedef {
    int array[MaxQ];
    int front;
    int back;
    int size;
} *Queue;


Queue init() {
    Queue q = malloc(sizeof(*q));
    if (q == NULL)
        return NULL;
    q->front = 0;
    q->back = 0;
    q->size = 0;
    return q;
}

int enqueue(Queue q, int v) { 
    if (q == NULL)
        return 0;
    if(q->size==MaxQ)
        return 0;
    q->array[q->back] = v;
    q->back++;
    if(q->back == MaxQ)
        q->back = 0;
    q->size++;
    return 1;

}

int dequeue(Queue q, int *v) {
    if (q == NULL)
        return 0;
    if(q->size == 0)
        return 0;
    *v = q->array[q->front];
    q->front++;
    if(q->front == MaxQ)
        q->front =0;
    q->size--;
    return 1;
}

int front(Queue q, int *v) {
    if (q == NULL)
        return 0;
    if(q->size==0)
        return 0;
    *v = q->array[q->front];
    return 1;
}

int isEmpty(Queue q) {
    if (q == NULL)
        return 0;
    if(q->size == 0)
        return 1;
    return 0;
}

int main(){
    Queue teste = init();
    if (teste == NULL)
        return -1;
    int aux;

    aux = -1;
    printf("Value : %d\n", aux);

    enqueue(teste,5);
    enqueue(teste,10);
    enqueue(teste,15);
    front(teste, &aux);

    printf("Next costumer: %d\n", aux );

    dequeue(teste, &aux);

    printf("Costumer %d left queue\n", aux );

    dequeue(teste, &aux);

    printf("Costumer %d left queue\n", aux );

    dequeue(teste, &aux);

    printf("Costume %d left queue\n", aux );

    int random = 10;
    if(random == 10)
        printf("asdasdasd\n");
    free(teste);

    return 0;
}

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182759

Your code can't decide whether a Queue is a pointer to something or the something.

typedef struct {
    int array[MaxQ];
    int front;
    int back;
    int size;
}*Queue;

This says a Queue is a pointer to a bunch of things.

Queue q = (Queue)malloc(sizeof(Queue));
q->front = 0;

This allocates enough bytes to hold a Queue, which is just a pointer, and then attempts to use the thing it points to. But you never allocated space for anything but a pointer.

Upvotes: 2

Related Questions