簡體   English   中英

在 C 程序中接收分段錯誤(核心轉儲)

[英]Receiving segmentation fault (core dumped) in C program

好的,所以,根據這篇文章,我知道我正在嘗試訪問我無法訪問的內存。 我唯一能想到的是我在哪里創建任務實例並為其分配值,但是,查看這篇文章,我看不出我的實現在哪里不正確。

任何幫助將不勝感激。

#include "task.h"

struct node{
    Task *task;
    struct node *next;
};

void insert(struct node **head, Task *task);
void delete(struct node **head, Task *task);
void traverse(struct node *head);
#ifndef TASK_H
#define TASK_H

typedef struct task{
    char *name;
    int tid;
    int priority;
    int burst;
} Task;

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

#include "list.h"
#include "task.h"
#include "schedulers.h"

void add(char *name, int priority, int burst){

     printf("beginning of add\n"); // Just a test. I never see this message print
     static int x = 1;
     struct node *list_head = NULL;
     Task xtask = (Task) { .name = name, .tid = x, .priority = priority, .burst = burst};
     insert(&list_head, &xtask);
     x++;
}

void schedule(){
     printf("test"); // just a place holder for now
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "task.h"
#include "list.h"
#include schedulers.h"

#define SIZE 100

int main(int argc, char ** argv[]){


     FILE *in;
     char *temp;
     char task[SIZE];

     char *name;
     int priority;
     int burst;

     in = fopen(argv[1], "r");
     if (argv[1] != NULL){
     while (fgets(task, SIZE, in) != NULL){
          temp = strdup(task);
          name = strsep(&temp, ",");
          priority = atoi(strsep(&temp, ","));
          burst = atoi(strsep(&temp, ","));

          add(name, priority, burst);
          free(temp);
     }
     fclose(in);
     }
     else{ 
          printf("invalid\n");
          return 0;
     }
     schedule();

     return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "list.h"
#include "task.h"

void insert(struct node **head, Task *newTask){
    struct node *newNode = malloc(sizeof(struct node));

    newNode ->task = newTask;
    newNode ->next = *head;
    *head = newNode;
}

void delete(struct node **head, Task *task){

     struct node *temp;
     struct node *prev;

     temp = *head;
     if (strcmp(task ->name, temp -> task -> name) == 0){
          *head = (*head) -> next;
     }

     else {
          prev = *head;
          temp = temp -> next;
          while (strcmp(task -> name, temp -> task -> name) != 0) {
               prev = temp;
               temp = temp -> next;
          }
          prev -> next = temp -> next;
      }
}

void traverse(struct node *head){

      struct node *temp;
      temp = head;

      while (temp != NULL){
           printf("[%s] [%d] [%d]\n", temp-> task -> name, temp -> task -> priority, temp -> task -> burst);
      temp = temp -> next;
      }

}

這是一個非常大的問題:

void add(char *name, int priority, int burst){
     printf("beginning of add\n"); // Just a test. I never see this message print
     static int x = 1;  // <-- Since "x" is static, it will persist after you exit add()
     struct node *list_head = NULL;  // But both *list_head and xtask will be INVALID the moment you leave add()
     Task xtask = (Task) { .name = name, .tid = x, .priority = priority, .burst = burst};
     x++;

如果您嘗試在add()之外使用 list_head 和/或 xtask .. 這很容易成為您的分段違規的原因。

可能的解決方案:

add()之外聲明它們,並將它們作為參數傳入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM