0

I am trying to implement a FIFO. The code compiles without errors but i get segmentation fault when running the program. What is the problem?

#include <stdio.h>    
#include <stdlib.h>    
struct cell            
{
 int element;
 struct cell *next;
};
struct queue           
{
 struct cell *front;   
 struct cell *rear;    
};

void enqueue(int x, struct queue *Q);
void dequeue(struct queue *Q);

main()  
/* Manipulation of a linked queue of cells. */
{
 struct queue *Q;
 struct cell *q;
 int i;

 Q->front=Q->rear=NULL;
 for(i=0; i<8; i++) {enqueue(i+1, Q);} 
 printf("Q->front = %p,  Q->rear = %p\n", Q->front, Q->rear);
 q=Q->front;
 while(q!=NULL)
   {
    printf("cell = %d, %p\n", q->element, q->next);
    q=q->next;
   }  
 for(i=0; i<10; i++) dequeue(Q);
 printf("Q->front = %p,  Q->rear = %p\n", Q->front, Q->rear);
 q=Q->front;
 while(q!=NULL)
   {
    printf("cell = %d, %p\n", q->element, q->next);
    q=q->next;
   } 
 return(0);
}

void enqueue(int x, struct queue *Q)

{
 struct cell *p;

 p=(struct cell *)malloc(sizeof(struct cell)); 
 if(Q->rear != NULL) Q->rear->next = p;        
 Q->rear = p;
 if(Q->front == NULL) Q->front = p;            
 Q->rear->element = x; Q->rear->next = NULL;
 return;
}

void dequeue(struct queue *Q)
{
 struct cell *q;

 if(Q->front == NULL) {printf("Error: Queue is empty.\n"); exit(1);} 

 else {q=Q->front; Q->front = Q->front->next; free(q);} 
 if(Q->front == NULL) Q->rear = NULL;
 return;
}
2
  • 1
    The problem is that your use of single-letter variable names, some distinguished only by case, has made the code almost impossible to work on. Crashing bugs are the first and most obvious fall-out from that original problem. Commented Jul 5, 2011 at 7:56
  • -1: It would be nice if you'd tell us WHERE in the program you get the seg-fault? Commented Jul 5, 2011 at 8:09

2 Answers 2

3

You are not allocating memory for Q and using it right away:

struct queue *Q; /* Where does Q point ? */

Q->front=Q->rear=NULL; /* Q not initialized, undefined behavior */

So Q points to some random value on the stack. To solve it, use malloc:

struct queue *Q;
Q = malloc(sizeof(*Q));
if (NULL == Q) {
    /* Tinfoil hat. */
}
Sign up to request clarification or add additional context in comments.

2 Comments

Q = malloc(sizeof(*Q)); ?? :)
@Donotalo Yup :-) I always do it like that (perhaps I want to change the type of Q later?) + less to type.
0

You have no space allocated to your struct queue *Q;

 struct queue *Q;
 struct cell *q;
 int i;

 Q->front=Q->rear=NULL;

Q is just an uninitialized pointer. It doesn't point to anything valid, so you can't dereference it as you do in Q->front=Q->rear=NULL;

Change it to allocate Q on the stack, and pass its address to your functions.

 struct queue Q;
 struct cell *q;
 int i;

 Q.front=Q.rear=NULL;
 for(i=0; i<8; i++) {enqueue(i+1, &Q);} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.