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