0

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

}
Threshold19
  • 107
  • 7
  • I can't see how the code would crash, but I don't see how it could work, either. You fill a local struct with data and never use it again. Perhaps the error is before or how you call `add`. Please show that code. – M Oehm Mar 29 '20 at 06:04
  • Okay. One moment – Threshold19 Mar 29 '20 at 06:05
  • Oh my word. That isn't even correct. I left out another function. A few moments. – Threshold19 Mar 29 '20 at 06:06
  • Make a propoer [mre] while you are at it. – Yunnosch Mar 29 '20 at 06:06
  • Okay, I can add more. I didn't include anything else because everything else is professor provided. This was my only contribution. I will keep adding as needed. – Threshold19 Mar 29 '20 at 06:11
  • 1
    "I will keep adding as needed." That's tedious. You still don't show how you call `add`. Create a minimal example and show it completely, otherwise we can just guess what's wrong. – M Oehm Mar 29 '20 at 06:23
  • 1
    I'm adding everything currently. Thank you for your input. – Threshold19 Mar 29 '20 at 06:25
  • Sorry everyone. It's added now. – Threshold19 Mar 29 '20 at 06:38
  • 1
    There are many possibilites for seg faults before `add` is called: `argv[1]` might be `NULL` and therefore an invalid argument for `fopen`; All calls to `strsep` can return `NULL`, which will crash when passed to `atoi`. Impossible to know what happens without knowing how you invoke your program or what the contents of the file opened with `fopen` are. In any case you should protect your code from crashing by ensuring non-null arguments. – M Oehm Mar 29 '20 at 06:44
  • Thanks a lot. I'm going to address each of these now. – Threshold19 Mar 29 '20 at 06:47
  • I added an if argv[1] != NULL condition to invoke the while loop. else { print invalid and return 0. When run, it says invalid and exits. Does that mean that the seg fault was definitely the argv[1]? *I edited the code to reflect this change* – Threshold19 Mar 29 '20 at 06:59
  • 1
    `argv[]` are the arguments passed to the program. Your program requires one argument, namely the name of a file from which to read the tasks. That can be done by evoking `program filename` in a shell or by dragging the file icon on the program icon in a file manager. If you run from a development evironment, there will be an option to specify the arguments. – M Oehm Mar 29 '20 at 07:04
  • Alright. That must be it. I'm running the makefile, then ./(target). *edit* Yes. When doing ./target file, it works. Thanks a lot. – Threshold19 Mar 29 '20 at 07:07
  • 1
    You're welcome. But look how far we had to go from your initial post to get at the source of the problem. – M Oehm Mar 29 '20 at 07:10
  • I know. I feel so stupid. – Threshold19 Mar 29 '20 at 07:11
  • Don't. Just a gentle reminder that the problem may not be where you look at first ... – M Oehm Mar 29 '20 at 07:15

1 Answers1

0

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.

POTENTIAL SOLUTION:

Declare them outside of add(), and pass them in as parameters.

FoggyDay
  • 11,962
  • 4
  • 34
  • 48