简体   繁体   English

在 C 程序中接收分段错误(核心转储)

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

Okay, so, according to this post , I understand that I'm trying to access memory that I do not have access to.好的,所以,根据这篇文章,我知道我正在尝试访问我无法访问的内存。 The only thing I can think of would be where I'm creating an instance of task and assigning it values, but, looking at this post, I can't see where my implementation is incorrect.我唯一能想到的是我在哪里创建任务实例并为其分配值,但是,查看这篇文章,我看不出我的实现在哪里不正确。

Any help would be greatly appreciated.任何帮助将不胜感激。

#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;
      }

}

Here's one very big problem:这是一个非常大的问题:

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++;

If you're trying to use list_head and/or xtask outside of add() .. that could easily be the cause of your segmentation violation.如果您尝试在add()之外使用 list_head 和/或 xtask .. 这很容易成为您的分段违规的原因。

POTENTIAL SOLUTION:可能的解决方案:

Declare them outside of add(), and pass them in as parameters.add()之外声明它们,并将它们作为参数传入。

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

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