Thread: Understanding structs and how to use them

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    2

    Understanding structs and how to use them

    I have been trying to understand how to use a struct that is in a header file. The code looks like this:

    Code:
    /**
     * Representation of a task in the system.
     */
    
    
    #ifndef TASK_H
    #define TASK_H
    
    
    // representation of a task
    typedef struct task {
        char *name;
        int tid;
        int priority;
        int burst;
    } Task;
    
    
    #endif
    now in my file i have one function that is setup to add given variables into a linked list using the struct. I can get the information into the struct but i don't know how to get the information to go into the other functions. here is the code i have written:
    Code:
    //Last Date Modified: 3/27/2020                                                                                                                                                                                                       
    //File Name: schedule_fcfs.c                                                                                                                                                                                                          
    //Description: This is the algorithm that produces the first come first server schedule setup                                                                                                                                         
    
    
    #include <stdlib.h>
    #include <stdio.h>
    #include "schedulers.h"
    #include "task.h"
    #include "list.h"
    #include "cpu.h"
    
    
    void add(char *name, int priority, int burst)
    {
      Task *taskNew = malloc(sizeof(Task));
      struct node **head = malloc(sizeof(**head));
      int value=0;
      __sync_fetch_and_add(&value,1);
      taskNew->name = name;
      taskNew->tid=value;
      taskNew->priority=priority;
      taskNew->burst=burst;
      insert(head, taskNew);
      //use insert from list.h                                                                                                                                                                                                            
    }
    
    
    void pickNextTask()
    {
      struct node *head = malloc(sizeof(*head));
      traverse(head);
    }
    
    
    void schedule()
    {
      struct node **head = malloc(sizeof(**head));
      while(head != NULL){
        int slice = taskNew->burst;
        run(task, slice);
        pickNextTask();
        schedule();
      }
    }
    and here is the code for the insert function.

    Code:
    // add a new task to the list of tasks
    void insert(struct node **head, Task *newTask) {
        // add the new task to the list 
        struct node *newNode = malloc(sizeof(struct node));
    
    
        newNode->task = newTask;
        newNode->next = *head;
        *head = newNode;
    }
    whenever i run this i get seg faults which is due to the information not being there but i don't know how to get it there, I also know that this code is horribly setup aswell.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,972
    Your 'struct node head' thing needs to travel between all the functions.

    All your add() function does is create an empty list, append one task to it, then throw the whole lot away with a memory leak.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    2
    That makes sense but i don't understand how to fix that. Where do i create the instance of the struct and where do i get the information to save and be able to be used?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,972
    Maybe things like
    Code:
    void add(node **head, char *name, int priority, int burst)
    {
      Task *taskNew = malloc(sizeof(Task));
    //  int value=0;
    //  __sync_fetch_and_add(&value,1);
    // no idea why this is here, operating on a local no one else can see.
      taskNew->name = name;
      taskNew->tid=value;
      taskNew->priority=priority;
      taskNew->burst=burst;
      insert(head, taskNew);
      //use insert from list.h                                                                                                                                                                                                            
    }
    and
    Code:
    void schedule(struct node *head)
    {
      node *temp = head;
      while(temp != NULL){
        int slice = temp->task->burst;
        run(task, slice);
        // ?? pickNextTask();
        temp = temp->next;
      }
    }

    In main, you have
    Code:
    node *head = NULL;  // empty
    add(&head,"t1",22,33);
    schedule(head);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble understanding malloc + structs
    By Finoli in forum C Programming
    Replies: 15
    Last Post: 01-03-2016, 07:45 AM
  2. Replies: 2
    Last Post: 01-08-2013, 07:55 AM
  3. Typedef Structs inside Typdef structs
    By gremory in forum C Programming
    Replies: 21
    Last Post: 12-30-2011, 07:48 PM
  4. Help understanding typedef structs
    By difficult.name in forum C Programming
    Replies: 3
    Last Post: 09-22-2004, 12:43 AM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM

Tags for this Thread