【问题标题】:New to C trying to make a Round Robin. Realloc():Invalid next size issueC 新手尝试进行循环。 Realloc():无效的下一个大小问题
【发布时间】:2020-11-08 02:48:17
【问题描述】:

我的问题是我相信我的代码是正确的,但是,我继续得到 realloc(): invalid next size。我已经查找了如何解决 realloc 问题,但仍然遇到问题。我试图确保释放这些值。因为我不知道订单队列的数量,所以我使用 malloc。它遍历给定的突发值队列并将它们与量子进行比较。该代码应该返回队列的顺序、周转时间的队列和总处理时间。我在哪里感到困惑/错误?

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

【问题讨论】:

  • 这通常意味着您已经超出了缓冲区或在释放后使用了分配的区域。尝试使用 valgrind 等工具快速找到这些。
  • 如果您要发布一个可以编译和运行的完整程序minimal reproducible example(主要功能,#includes,等等),有人可能愿意为您测试它.请注意,malloc 错误通常与触发错误的函数位于完全不同的位置,因此完整的示例也很重要;该错误可能在您未向我们展示的代码中。
  • 我不能说我已经对您的代码进行了详细分析,但是在很多情况下,您的代码如result-&gt;order = bigorder; 紧跟free(bigorder);。这些对我来说似乎是代码的味道:如果您要立即使该指针无效(通过释放它),那么将指针分配给某物有什么意义。
  • 是的,更糟糕​​的是:如果您返回该指针,调用者很可能会使用它。
  • @Nate ...很可能会尝试使用它。 ;)

标签: c realloc round-robin


【解决方案1】:
           bigorder = (int *) realloc(neworder, (newlen*newlen)* sizeof(int));
           free(neworder);

这是错误的。在传递一个指向realloc 的指针后,不要释放它(除非realloc 失败,你应该测试它)。完成后您可以释放bigorder

【讨论】:

  • 但即使我拿走了所有的 free(),我仍然得到 realloc(): invalid size。有小费吗?会不会是我做的不够大?我想弄清楚它有多大,但不确定如何
  • 正如我之前提到的,您需要创建一个minimal reproducible example。对于可重现的错误,盯着代码并不是找到它的有效方法;应该使用适当的调试工具运行它。所以首先,你应该说出你在这个方向上做了哪些尝试。这里的人可能愿意代表您这样做,但他们不能没有完整的代码。
猜你喜欢
  • 2014-10-13
  • 2020-12-27
  • 1970-01-01
  • 2015-01-22
  • 1970-01-01
  • 2018-06-13
  • 1970-01-01
相关资源
最近更新 更多