Thread: segmentation fault

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Unhappy segmentation fault

    I have a problem. I'm doing a project for my C and Unix class that involves queue's. Anyways, I have written a queue.c file which has funtions such as Enqueue, Dequeue, ShowQueue, and also a main function so that I can test to make sure this queue works. Anyways, I am getting a segmentation fault, and I'm not sure how to fix it. I have attached the code, so if anybody could take a quick look at it and tell me what I should look for when fixing it, I will be extremely grateful...

    By the way, at the top there is an included file queue.h which has structure and function declarations.

    Thanks...

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A segmentation fault is usually caused by dereferencing either a NULL pointer or an uninitialized pointer. If you step through your code then you should be able to easily find where you are causing the fault. From there tracing the fault to its source is relatively simple with such short code.

    p.s. You'll get better answers if you include complete compilable code so we don't have to make up declarations, that's a waste of time. Break the program down as small as you can while still maintaining the same problem so we don't have to debug reams of source

    Most of the time you can find the problem yourself simply by preparing a test case program for these boards.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    Cool, I will work with it some more. Thanks for your help...

    (PS, sorry about the code, I was running Linux and I couldn't get the code to copy and paste properly)

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    I've been struggling with this for hours, and I can't figure out where the problem is. I've added print statements all over the place to figure out where the problem is, and I think the problem lies in my Enqueue function, although I'm not for sure. Here is the code, which is compilable:

    #include "queue.h"

    void InitQueue(QUEUE *pQ){
    pQ->front = NULL;
    pQ->rear = NULL;
    }

    BOOLEAN IsEmpty(QUEUE *pQ){
    if(pQ -> front == NULL)
    return TRUE;
    return FALSE;
    }

    BOOLEAN Dequeue(int *pId, double *pt, QUEUE *pQ){
    struct NODE *temp;

    if(IsEmpty(pQ))
    return FALSE;
    temp = pQ -> front;
    pQ -> front = temp -> next;
    *pId = temp -> id;
    *pt = temp -> arrival;
    free(temp);
    return TRUE;
    }

    BOOLEAN Enqueue(int id, double t, QUEUE *pQ){
    struct NODE *newNode;
    printf("HELLO");
    if(newNode = malloc(sizeof(struct NODE)) != NULL){
    printf("Hello");
    newNode -> id = id;
    newNode -> arrival = t;
    newNode -> next = NULL;
    pQ -> rear -> next = newNode;
    pQ -> rear = newNode;
    return TRUE;
    }
    printf("Error allocating space, aborting program!!!");
    return FALSE;

    }

    void ShowQueue(QUEUE *pQ){
    struct NODE *temp;
    temp = pQ -> front;
    while(temp != NULL){
    printf("ID: &d\tArrival: %f \n", temp -> id, temp -> arrival);
    temp = temp -> next;
    }
    }

    main(){
    QUEUE myQueue;
    InitQueue(&myQueue);
    Enqueue(12, 12.3, &myQueue);
    Enqueue(17, 34.5, &myQueue);
    ShowQueue(&myQueue);
    return 0;
    }

    Here is "queue.h":

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

    #define TRUE 1
    #define FALSE 0
    typedef int BOOLEAN;



    struct NODE{
    double arrival;
    int id;
    struct NODE* next;
    };

    typedef struct{
    struct NODE *front, *rear;
    } QUEUE;

    void InitQueue(QUEUE *pQ);
    BOOLEAN IsEmpty(QUEUE *pQ);
    BOOLEAN Dequeue(int *pId, double *pt, QUEUE *pq);
    BOOLEAN Enqueue(int id, double t, QUEUE *pQ);
    void ShowQueue(QUEUE *pQ);

    If anybody knows how to fix this, I will do anything for you!!! (well, almost anything)

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This is the line that causes the error, I don't have time to debug it properly right now, but this will give you a start.
    In the Enqueue function:
    pQ -> rear -> next = newNode;

    -Prelude
    My best code is written with the delete key.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    In Enqueue:

    >pQ -> rear -> next = newNode;

    pQ->rear is NULL.

    NULL->next is giving you a segfault.


    edit: hey Prelude seems we saw it at the same time...
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    It works!!!

    I got it to work, thanks guys for your help!!! I appreciate it a lot!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM