2021年3月17日星期三

C - Freeing memory allocated with malloc

I'm programming in C and have a function insert() that allocates memory with malloc(). This function is called from another function named SJF(). The SJF() function calls insert() multiple times.

I'm wondering where and how I should free() this memory. I don't want to free the memory every time so doing so inside the insert() function isn't going to work. So would the best thing be to return the allocated memory from the function and then free it that way?

void insertJob (struct node **head, int key, int data) {      struct node * new_node = NULL;      struct node * last = NULL;        /*Create the new node*/      new_node = (struct node *)malloc(sizeof(struct node));        if (new_node == NULL)      {          printf("Failed to insert element. Out of memory");          return;      }        new_node->key = key;      new_node->data = data;      new_node->origBurst = data;      new_node->next = NULL;        /*No element in the linked list. So point head to the new node*/      if( *head == NULL)      {          *head = new_node;          return;      }        /*Traverse to the last node*/      last = *head;      while(last->next) last = last->next;        /*Point last node's link (next pointer) to the new node*/      last->next = new_node;        return;    }  
void sjf(int count, const int *submit, const int *burst) {       int response = 0, wait = 0, turnaround = 0;   struct node nd;   nd.minJob = 0;     // Create job index and clock    int job = 0, clock = 0, i = 0;      // Loop through all the jobs (SJF)    while(job<count) {              // Update clock as necessary           if(clock < submit[job]) {                   clock = submit[job];           }              // Bubble up the shortest job in the ready queue              // Prepare the ready Queue          for(int j = i; j < count; j++) {                  if(submit[j] <= clock) {                            insertJob(&head, submit[j],burst[j]);                          i++;                  }          }            // Find shortest job from ready queue           shortestJob(head, &nd);               // Calculate the next event (job end)            int nextevent = clock + nd.minData;                // Calculate statistics            int myresp = clock - nd.minJob;            int myta = nextevent - nd.minJob;            int mywait = myta - nd.minData;            response += myresp;            turnaround += myta;            wait += mywait;              // Set clock to the next event time            clock = nextevent;              // Increment job            job++;              // Loop back to top            removeJob(nd.minJob, nd.minData);    }      printf("Shortest Job First Scheduler\n");    printf("Resp: %.2f, Wait: %.2f, T/A: %.2f\n", (float) response / count,                                                  (float) wait / count,                                                  (float) turnaround / count);       //possibly free() memory here  }  

This code is just a snippet.

https://stackoverflow.com/questions/66683114/c-freeing-memory-allocated-with-malloc March 18, 2021 at 08:21AM

没有评论:

发表评论