Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 运行时错误-变量arr_进程周围的堆栈已损坏_C++_C - Fatal编程技术网

C++ 运行时错误-变量arr_进程周围的堆栈已损坏

C++ 运行时错误-变量arr_进程周围的堆栈已损坏,c++,c,C++,C,此代码用于为操作系统生成队列 我使用结构来实现我的流程 并使用arr_进程来处理所有这些进程 新的_进程数组根据其到达时间对这些进程进行排序 但当我在VisualStudio2010上运行此代码时 它会产生此运行时错误 运行时检查失败#2-变量arr#u进程周围的堆栈已损坏 这是密码 #include <stdio.h> #include <stdlib.h> typedef struct { int id; int arr_time; int

此代码用于为操作系统生成队列 我使用结构来实现我的流程 并使用arr_进程来处理所有这些进程 新的_进程数组根据其到达时间对这些进程进行排序

但当我在VisualStudio2010上运行此代码时 它会产生此运行时错误 运行时检查失败#2-变量arr#u进程周围的堆栈已损坏

这是密码

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    int id;
    int arr_time;
    int serv_time;
    int deadline;
} process;

void print_process(process n);
int main()
{
    process arr_processes[8];
    process new_processes[8];
    process real_processes[3];
    process ready_processes[5];
    process tmp_process[1];
    int length_ready;
    int i,length,j;
    int length_real;

    arr_processes[0].id=1;
    arr_processes[0].arr_time=12;
    arr_processes[0].serv_time=4;
    arr_processes[0].deadline=0;

    arr_processes[1].id=2;
    arr_processes[1].arr_time=10;
    arr_processes[1].serv_time=5;
    arr_processes[1].deadline=0;

    arr_processes[2].id=3;
    arr_processes[2].arr_time=9;
    arr_processes[2].serv_time=2;
    arr_processes[2].deadline=0;

    arr_processes[3].id=4;
    arr_processes[3].arr_time=8;
    arr_processes[3].serv_time=4;
    arr_processes[3].deadline=10;

    arr_processes[4].id=5;
    arr_processes[4].arr_time=5;
    arr_processes[4].serv_time=2;
    arr_processes[4].deadline=8;

    arr_processes[5].id=6;
    arr_processes[5].arr_time=3;
    arr_processes[5].serv_time=3;
    arr_processes[5].deadline=0;

    arr_processes[6].id=7;
    arr_processes[6].arr_time=2;
    arr_processes[6].serv_time=3;
    arr_processes[6].deadline=0;

    arr_processes[7].id=8;
    arr_processes[7].arr_time=1;
    arr_processes[7].serv_time=1;
    arr_processes[7].deadline=28;

    length=sizeof(arr_processes)/sizeof(arr_processes[0]);

    printf("\t length of the processes=%i\n\n",length);
    printf("\t The Original processes \n\n");
    for(i=0;i<8;i++)
        print_process(arr_processes[i]);

    // now we want to sort the processes according to their arrival time
    for(i=0;i<8;i++)
    {
        new_processes[i]=arr_processes[i];
    }

    for(i=0;i<length;i++)
    {
        for(j=0;j<length-i;j++)
        {
            if((new_processes[j].arr_time)>(new_processes[j+1].arr_time))
            {
                tmp_process[0]=new_processes[j];
                new_processes[j]=new_processes[j+1];
                new_processes[j+1]=tmp_process[0];
            }
        }
    }

    printf("\t The New processes \n\n");
    for(i=0;i<8;i++)
        print_process(new_processes[i]); // the new queue

    ready_processes[0]=arr_processes[0];
    ready_processes[1]=arr_processes[1];
    ready_processes[2]=arr_processes[2];
    ready_processes[3]=arr_processes[5];
    ready_processes[4]=arr_processes[6];

    length_ready=sizeof(ready_processes)/sizeof(ready_processes[0]);
    // now we want to design the ready queue
    for(i=0;i<length_ready;i++)
    {
        for(j=0;j<length_ready-i;j++)
        {
            if((ready_processes[j].arr_time)>ready_processes[j+1].arr_time)
            {
                tmp_process[0]=ready_processes[j];
                ready_processes[j]=ready_processes[j+1];
                ready_processes[j+1]=tmp_process[0];
            }
        }
    }

    printf("\t The ready processes \n\n");
    for(i=0;i<length_ready;i++)
    print_process(ready_processes[i]); // the ready  queue

    // now we want to design the ready real queue for the shortest deadline first
    // we donnot need to check for the new proesses at each instant of time
    //but we need to check for the service time from now

    real_processes[0]=arr_processes[3];
    real_processes[1]=arr_processes[4];
    real_processes[2]=arr_processes[7];

    length_real=sizeof(real_processes)/sizeof(real_processes[0]);
    for(i=0;i<length_real;i++)
    {
        for(j=0;j<length_real-i;j++)
        {
            if((real_processes[j].deadline)>real_processes[j+1].deadline)
            {
                tmp_process[0]=real_processes[j];
                real_processes[j]=real_processes[j+1];
                real_processes[j+1]=tmp_process[0];
            }
        }
    }

    printf("\t The real processes \n\n");
    for(i=0;i<length_real;i++)
    print_process(real_processes[i]); // the ready real queue

    // removed real process
    process removed_real;
    removed_real.id=0;
    removed_real.arr_time=0;
    removed_real.serv_time=0;
    removed_real.deadline=0;

    process running_process;
    running_process.id=0;
    running_process.arr_time=0;
    running_process.serv_time=0;
    running_process.deadline=0;

    int counter=0;
    int start_time;

    while(counter<=28)
    {
        printf("when time = %i\n\n",counter);
        // printf("\t The real processes when the counter=%i \n\n",counter);
        // for(i=0;i<length_real;i++)
        // print_process(real_processes[i]); // the ready real queue


        // first we must check for the real processes
        for(i=0;i<length_real;i++)
        {
            if((counter==real_processes[i].arr_time)
            &&((real_processes[i].deadline)-counter)>=(real_processes[i].serv_time))
            {
                running_process=real_processes[i];
                printf("The non zero deadline process is:%i\n",running_process.id);
                real_processes[i]=removed_real;
                start_time=counter;   // real process

                while(counter!=(start_time+running_process.serv_time))
                {
                    printf("At time = %i,The Running Process is...\n",counter);
                    print_process(running_process);
                    counter++;
                }
            }
        }

        counter++;
    }

    return 0;
}

void print_process(process n)
{
    if(n.deadline!=0)
    printf("ID=%i\narr_time=%i\nserv_time=%i\ndeadline=%i\n\n\n",n.id,n.arr_time,n.serv_time,n.deadline);
    else if(n.deadline==0)
    printf("ID=%i\narr_time=%i\nserv_time=%i\n\n\n",n.id,n.arr_time,n.serv_time);
}
#包括
#包括
类型定义结构
{
int-id;
国际到达时间;
国际服务时间;
int截止日期;
}过程;
作废打印流程(流程n);
int main()
{
过程arr_过程[8];
处理新的_过程[8];
过程实_过程[3];
过程就绪_过程[5];
工艺tmp_工艺[1];
int长度_就绪;
int i,长度,j;
int-length_-real;
arr_进程[0]。id=1;
arr_进程[0]。arr_时间=12;
arr_进程[0]。服务时间=4;
arr_进程[0]。截止日期=0;
arr_进程[1]。id=2;
arr_进程[1]。arr_时间=10;
arr_进程[1]。服务时间=5;
arr_进程[1]。截止日期=0;
arr_进程[2]。id=3;
arr_进程[2]。arr_时间=9;
arr_进程[2]。服务时间=2;
arr_进程[2]。截止日期=0;
arr_进程[3],id=4;
arr_进程[3]。arr_时间=8;
arr_进程[3]。服务时间=4;
arr_过程[3]。截止日期=10;
arr_进程[4],id=5;
arr_进程[4]。arr_时间=5;
arr_进程[4]。服务时间=2;
arr_进程[4]。截止日期=8;
arr_进程[5],id=6;
arr_进程[5],arr_时间=3;
arr_进程[5]。服务时间=3;
arr_进程[5]。截止日期=0;
arr_进程[6],id=7;
arr_进程[6]。arr_时间=2;
arr_进程[6]。服务时间=3;
arr_进程[6]。截止日期=0;
arr_进程[7].id=8;
arr_进程[7]。arr_时间=1;
arr_进程[7]。服务时间=1;
arr_过程[7]。截止日期=28;
长度=sizeof(arr_进程)/sizeof(arr_进程[0]);
printf(“\t进程的长度=%i\n\n”,长度);
printf(“\t原始进程\n\n”);

对于(i=0;i,当您超出数组的边界时,您会得到这些类型的错误

for(i=0;i<length;i++)
 {
       for(j=0;j<length-i;j++)
       {
             if((new_processes[j].arr_time)>(new_processes[j+1].arr_time))
             {
                   tmp_process[0]=new_processes[j];
                   new_processes[j]=new_processes[j+1] ;
                   new_processes[j+1]=tmp_process[0] ;
             }
       }
 }

for(i=0;i当您超出数组的边界时,会出现此类错误

for(i=0;i<length;i++)
 {
       for(j=0;j<length-i;j++)
       {
             if((new_processes[j].arr_time)>(new_processes[j+1].arr_time))
             {
                   tmp_process[0]=new_processes[j];
                   new_processes[j]=new_processes[j+1] ;
                   new_processes[j+1]=tmp_process[0] ;
             }
       }
 }

对于(i=0;i当索引用完时,下面是一个排序示例:

for(i=0; i<length - 1; i++)
{
     for(j=i + 1;j<length;j++)
     {
         if((new_processes[j].arr_time)>(new_processes[i].arr_time))
         {
              tmp_process[0]=new_processes[j];
              new_processes[j]=new_processes[i] ;
              new_processes[i]=tmp_process[0] ;
         }
    }
}
定义比较函数:

void qsort(void *base, size_t nmemb, size_t size,
       int (*compar)(const void *, const void *));
int compare_by_arr_time(const void* a, const void* b)
{
    int a_int = ((const process*)a)->arr_time;
    int b_int = ((const process*)b)->arr_time;

    return a_int - b_int;  // or b_int - a_int
}
并按如下方式使用:

qsort(new_processes,
      sizeof(new_processes)/sizeof(new_processes[0]),
      sizeof(new_processes[0]),
      compare_by_arr_time);

索引用完时,下面是一个排序示例:

for(i=0; i<length - 1; i++)
{
     for(j=i + 1;j<length;j++)
     {
         if((new_processes[j].arr_time)>(new_processes[i].arr_time))
         {
              tmp_process[0]=new_processes[j];
              new_processes[j]=new_processes[i] ;
              new_processes[i]=tmp_process[0] ;
         }
    }
}
定义比较函数:

void qsort(void *base, size_t nmemb, size_t size,
       int (*compar)(const void *, const void *));
int compare_by_arr_time(const void* a, const void* b)
{
    int a_int = ((const process*)a)->arr_time;
    int b_int = ((const process*)b)->arr_time;

    return a_int - b_int;  // or b_int - a_int
}
并按如下方式使用:

qsort(new_processes,
      sizeof(new_processes)/sizeof(new_processes[0]),
      sizeof(new_processes[0]),
      compare_by_arr_time);

您的内部循环(使用
j
)使用
j+1
索引,当
i==0
时,这是超出范围的,那么内部循环索引会有什么变化?!您能用示例行向我表示感谢您的内部循环(使用
j
)使用
j+1
索引,当
i==0
时,这是超出范围的,那么内部循环索引会有什么变化?!你能用示例行提前向我表示感谢吗?当我认为消息提到“arr_进程”时,我更正了因为我们可能无意中写入了该数组的内存?如果不是,为什么消息不是针对“新的_进程”的这可能是对的。通过超越
新的\u进程的界限,我们可以修改
arr\u进程
数组。声明顺序可能是一个因素,或者至少,这是我从斯坦福大学的一个编程范例视频中获得的。非常感谢,我得到了消息,问题得到了解决d再次感谢:)当我认为该消息提到“arr\u进程”是因为我们可能会意外地在该数组的内存中写入时,我是对的吗?如果不是,为什么该消息不是针对“new\u进程”的这可能是对的。通过超越
新的\u进程的界限,我们可以修改
arr\u进程
数组。声明顺序可能是一个因素,或者至少,这是我从斯坦福大学的一个编程范例视频中获得的。非常感谢,我得到了消息,问题得到了解决d再次感谢:)