0

My question is I believed my code was correct, however, I continue to get a realloc(): invalid next size. I've looked up how to fix realloc issues, but still getting the issue. I tried to make sure to free the values. Since I won't know the amount to do for the order queue, I'm using malloc. It iterates through a given queue of burst values and compares them to the quantum. The code is supposed to return the order of the queue, a queue of the turnaround times, and the total process time. Where am I getting confused/wrong?

rr_result *rr(int *queue, int np, int tq)
{
 rr_result *result = malloc(sizeof(rr_result));
   result->np= np;
   result->turnarounds = malloc(sizeof(int) * np);

   // code here to assign values to result->turnarounds, result->order, and result->order_np
   int temp[np], curr, check[np], startp[np], flag, newlen, *neworder=NULL, *bigorder=NULL, *turna=NULL;
   neworder= (int *) malloc(sizeof(int) * np);
   turna = (int *) malloc(sizeof(int) * np);

   for(int i = 0; i < np; i++){ // Makes an array of the burst times we can use to update
       temp[i]= queue[i];
       check[i] = 0;
   }
   curr = 0; //The current time value of the array
   newlen = 0; //Length for the results array
   while (1)
   {
       flag = 0; //To exit out of the infinite rr loop
       for (int i = 0; i < np; i++)
       {
           if (temp[i]>0) //Check if there is still burst time left
           {
               flag = 1; //Something still must be processed
               if (check[i] == 0)
               {
                   startp[i] = curr; //Save the process start time
                   check[i] = 1; // Save the flag that this specific process has started
               }
               if (temp[i] > tq)
               {
                   curr += tq; //Update current time value
                   temp[i] -= tq; //Decrease the burst time by the quantum value
               }else
               {
                   curr += temp[i]; //Update current time value
                   turna[i] = curr - startp[i]; //Calculate the turnaround value by subtracting the start time from current time
                   temp[i]= 0; //Update to show the process is finished
               }
           }
           if(newlen > np){
               bigorder = (int *) realloc(neworder, (newlen*newlen)* sizeof(int));
               free(neworder);
               bigorder[newlen] = i;
               newlen++;
           }else{
               neworder[newlen] = i;
               newlen++;
           }
       }
       if (flag == 0)
       {
           if(bigorder != NULL){
               result->order = bigorder;
               free(bigorder);
           }else
           {
               result->order = neworder;
               free(neworder);
               free(bigorder);
           }
           result->turnarounds = turna;
           result->order_n = curr;
           free(turna);
           break;
       }
   }
   return result;
}

  • This usually means you have overrun a buffer or used an allocated region after it was freed. Try a tool such as valgrind to find these quickly. – Nate Eldredge Nov 08 '20 at 02:56
  • If you would post a [mcve] that is a full program that can be compiled and run (a main function, `#include`s, all that), someone might be willing to test it for you. Note that malloc bugs are often in a completely different place from the function where the error is triggered, so a complete example is important for that reason too; the bug may be in code you haven't shown us. – Nate Eldredge Nov 08 '20 at 02:57
  • 1
    I can't say I've made a *detailed* analysis of your code but there are numerous cases where you have code like `result->order = bigorder;` followed immediately by `free(bigorder);`. These seem like code smells to me: what's the point of assigning a pointer to something if you're immediately going to invalidate that pointer (by freeing it). – Adrian Mole Nov 08 '20 at 03:00
  • Yes, and worse: if you're returning that pointer, the caller is most likely going to use it. – Nate Eldredge Nov 08 '20 at 03:02
  • @Nate ...most likely going to *try to* use it. ;) – Adrian Mole Nov 08 '20 at 03:03

1 Answers1

0
           bigorder = (int *) realloc(neworder, (newlen*newlen)* sizeof(int));
           free(neworder);

This is wrong. After passing a pointer to realloc, do not free it (unless realloc failed, which you ought to test). You may free bigorder when done with it.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • But even when I take away all the free()'s I still get realloc(): invalid size. Any tips? Could it be I'm not making it big enough? I'm trying to figure out how big to make it, but not exactly sure how – JJBells16 Nov 08 '20 at 07:16
  • As I mentioned before, you need to create a [mcve]. For a reproducible bug, staring at the code is not an efficient way to find it; one should run it with appropriate debugging tools. So first, you should say what attempts you've made in that direction. People here may be willing to do that on your behalf, but they can't without complete code. – Nate Eldredge Nov 08 '20 at 16:11