简体   繁体   English

从队列弹出时C中的分段错误

[英]Segmentation fault in C while popping from a queue

I am facing a problem and I tried to debug it but I couldn't find anything 我遇到问题,尝试调试它,但找不到任何东西

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

struct Process {
    int id;
    int at;
    int bt;
    int rt;
    struct Process *next;
} *tmp;

struct Queue {
    struct Process *head, *tail;
};

struct Process *pop(struct Queue *queue) {
    if (queue->head == NULL) {
        return NULL;
    }
    tmp = queue->head;
    queue->head = queue->head->next;
    return tmp;
}

int main() {
    struct Queue queues[3];
    struct Process *working;
    for (int i = 0; i < 3; i++) {
        queues[i].head = NULL;
    }
    FILE *processesFile = fopen("processes.txt", "r");
    while (!feof(processesFile)) {
        fscanf(processesFile, "%d %d %d", &id, &at, &bt);
        push(&queues[0], id, at, bt);
    }
    working = pop(&queues[0]); // HERE IS THE PROBLEM, It is removing the first element but causing segmentation error
    return 0;
}

I removed the pushing part, I could successfully push 5 values and print the whole queue without any problems 我删除了推送部分,我可以成功推送5个值并打印整个队列而没有任何问题

I dont see the definition for push(&queues[0], id, at, bt);. 我没有看到push(&queues [0],id,at,bt);的定义。 fscanf(processesFile, "%d %d %d", &id, &at, &bt); fscanf(processesFile,“%d%d%d”,&id,&at,&bt); here the variables id,at and bt are used but they are not actually accessed as structure variables. 这里使用了变量id,at和bt,但实际上并未将它们作为结构变量进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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