1

I have a struct that contains pointer next. I am trying to make a queue of the structs, but when I call enqueue/dequeue I eventually get errors trying to write information; and don't know if it's due to enque/dequeue/insert. Just learning C and not really sure of free/malloc or if they're necessary. Insert is based on int value (smallest value first).

struct queue { 
    struct RCB* front; 
    struct RCB* back; 
};

/*Initialize queue */ 
struct queue* new_queue(void){
    struct queue* tmp = malloc( 1 * sizeof(tmp));  
    tmp->front = NULL; 
    tmp->back = NULL;
    return tmp;  
}

/*Add RCB to Queue FIFO */
struct queue* enqueue(struct queue* queue, struct RCB* rcb){
    if(queue->front == NULL && queue->back == NULL){
        queue->front = rcb; 
        queue->back = rcb;
            printf("added front to queue\n");  
        return queue; 
    }
    else {
        queue->back->next = rcb; 
        queue->back = rcb;
            printf("added to queue\n");  
    }
    return queue; 
} 

/*Remove RCB from Queue FIFO */ 
struct RCB* dequeue(struct queue* queue){
    struct RCB *tmp1 = (struct RCB *)malloc(sizeof(struct RCB)); 
    struct RCB *tmp2 = (struct RCB *)malloc(sizeof(struct RCB)); 
    if(queue->front == NULL && queue->back == NULL){
        printf("queue is empty\n"); 
    }   
    tmp1 = queue->front; 
    tmp2 = tmp1->next; 
    queue->front = tmp2; 
    if(queue->front == NULL){
        queue->back = queue->front;
            printf("removed rcb from queue\n");  
    }
    return tmp1; 
}
/*Insert RCB into Queue */ 
 struct queue* insert(struct queue* queue, struct RCB* rcb){
            if(queue->front == NULL && queue->back == NULL){
                queue->front = rcb; 
                queue->back = rcb;
                return queue; 
            }
            if(queue->front->next == NULL){
                queue->front->next = rcb; 
                return queue; 
            }

            struct RCB *tmp = (struct RCB *)malloc(sizeof(struct RCB));
                    tmp = queue->front; 
                        while(tmp->next->b2r < rcb->b2r || tmp->next == NULL){
                        tmp = tmp->next;
                    }
                    rcb->next = tmp->next; 
                    tmp->next = rcb; 

                    return queue; 
    }

Any help is greatly appreciated.

fluter
  • 13,238
  • 8
  • 62
  • 100
guy_sensei
  • 513
  • 1
  • 6
  • 21

1 Answers1

3
  1. Do not cast malloc return value

  2. in new_queue, struct queue* tmp = malloc( 1 * sizeof(tmp)); is wrong since this will allocate a buffer with size of pointer to struct queue, instead of the size of struct queue, also the 1*foo thing is not useful at all. Use following:

    struct queue* tmp = malloc(sizeof(*tmp));
    
  3. in dequeue, you are allocating two buffers of struct RCB while imediately lose the reference to them by assign to the pointers tmp1 and tmp1, those are not necessary, and you are creating memory leaks. What you need are just two pointers:

    struct RCB* dequeue(struct queue* queue) {
            struct RCB *tmp1; 
            struct RCB *tmp2; 
            if(queue->front == NULL && queue->back == NULL) {
                printf("queue is empty\n"); 
            }   
            tmp1 = queue->front; 
            tmp2 = tmp1->next; 
            queue->front = tmp2; 
            if(queue->front == NULL) {
                queue->back = queue->front;
                    printf("removed rcb from queue\n");  
            }
            return tmp1; 
    }
    
Community
  • 1
  • 1
fluter
  • 13,238
  • 8
  • 62
  • 100