简体   繁体   中英

Nodes creating a segmentation fault

I'm new to coding in c and I'm not very good with using pointers. The code compiles fine, but running it gives a segmentation fault. I've narrowed it down to where I create the Nodes (I think) in main, but I don't know how to fix it. Any help would be great! Thanks.

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

/* STRUCTURES */
struct Node
{
    int id;
    int at;
    struct Node* prev;
    struct Node* next;
};

struct Queue
{
    struct Node* head;
    struct Node* tail;
};


/* GLOBAL VARIABLES */
struct Queue* orderedArrival;           // holds original queue setup
struct Queue* waitQueue;                // changeable copy of above


/* METHODS */
void addByArrivalTimes(struct Node* process, struct Queue* queue); 
void printQueue(struct Queue* queue);


/* MAIN */
int main()
{
    orderedArrival = malloc(sizeof orderedArrival);
    if (orderedArrival == NULL)
        return 0;

    waitQueue = malloc(sizeof waitQueue);
    if (waitQueue == NULL)
        return 0;


    orderedArrival->head = orderedArrival->tail = NULL;
    waitQueue->head = waitQueue->tail = NULL;

    int i;

    for(i = 0; i < 10; i++) //10 processes
    {
        struct Node* process;
        process = malloc(sizeof *process);
        process->next = NULL;
        process->prev = NULL;
        process->id = i;
        process->at = rand()%10;

        addByArrivalTimes(process, orderedArrival);
    }
    printQueue(orderedArrival);

    return 1;
}


// Method is to put processes in order by arrival time
void addByArrivalTimes(struct Node* process, struct Queue* queue)
{
    // Queue is Empty
    if(queue->head == NULL)
    {
        queue->head = process;
        queue->tail = process;
    }

    // Queue is not Empty
    else
    {
        struct Node* q = queue->tail;
        struct Node* p = queue->head;

        // Add at end
        if(process->at > q->at)
        {
            queue->tail = process;
            process->prev = q;
            q->next = process;
        }

        // Add at beginning
        else if(p->at > process->at)
        {
            queue->head = process;
            process->next = p;
            p->prev = process;
        }

        // Add in the middle
        else
        {
            while(p->at < process->at)
                p = p->next;

            q = p->prev;
            q->next = process;
            p->prev = process;
            process->prev = q;
            process->next = p;
        }
    }
}

void printQueue(struct Queue* queue)
{
    struct Node* p = queue->head;
    while(p != NULL)
    {
        printf("Process: %d, Arrival Time: %d\n", p->id, p->at);
        p = p->next;
    }
}

In malloc you are using sizeof on a variable not a data type you need to change:

int main(int argc, char* argv[])
{
    orderedArrival = malloc(sizeof(struct Queue));
    if (orderedArrival == NULL)
        return 1;

    waitQueue = malloc(sizeof(struct Queue));
    if (waitQueue == NULL)
        return 1;
//...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM