C程序未正确计算条件

tkclm6bt  于 2023-03-22  发布在  其他
关注(0)|答案(1)|浏览(100)

我有一个程序,它应该重置某个valriable,如果给定数组中的第n +1个值是不存在的,即NULL,即使该值是null -这是通过print语句验证的,程序并不总是这样做。
等于null的值是next,它也等于(*globalnodeSet[Z1]).memPool[((*globalnodeSet[Z1]).pc)+ 1][0])

int startFunction(void* execState)
{
    while(1)
    {
        int *x = (int*)execState;
        if (*x == 1 )
        {
           for(;;)
           {
            // 1 second time gap between cycles
            Sleep(200);
            
            //program counter increments and so does the  active node
            Z1 ++; (*globalnodeSet[Z1]).pc++;(*globalnodeSet[Z1]).acc++;
            
            
            //logic loop

            printf("\nrunning - CCX:%u PC: %u\n",Z1 ,(*globalnodeSet[Z1]).pc);
            printf("the next instruction in memory is : %s\n",(*globalnodeSet[Z1]).memPool[((*globalnodeSet[Z1]).pc) + 1][0]);
            //if last core is reached
            if (Z1 == 11)
            {
                int counter = 0;
                for(counter = 0; counter <= 11; counter++)
                {
                    //reset loop and increment all program counters
                    //checks if next instruction exists at all
                    char *next = (*globalnodeSet[counter]).memPool[((*globalnodeSet[counter]).pc) + 1][0];
            
                    if(next == NULL)
                    {
                        //if no next instruction exists - set the program counter to -1 so that the first , 0th instruction will be executed on the next cycle.
                        (*globalnodeSet[counter]).pc = -1;
                    } else printf("not null\n");               
                }
                //loop counter is reset
                Z1 = - 1;
            } 

            //if the end of a node's instruction is reached , either pc = 11 or no next instruction exists
            if((*globalnodeSet[Z1]).pc == 11 || (*globalnodeSet[Z1]).memPool[((*globalnodeSet[Z1]).pc) + 1][0] == NULL)
            {
                //reset the program counter
                (*globalnodeSet[Z1]).pc = -1;
            }            
            //if stop button is pressed
            if (*x == 0) return 0;
            //we are using pointers so sim values are still retained upon function exit
           }           
        } 
    }
}

程序的这一部分的输出如下:

running - CCX:0 PC: 0
the next instruction in memory is : (null)

running - CCX:1 PC: 0
the next instruction in memory is : (null)

running - CCX:2 PC: 0
the next instruction in memory is : (null)

running - CCX:3 PC: 0
the next instruction in memory is : (null)

running - CCX:4 PC: 0
the next instruction in memory is : (null)

running - CCX:5 PC: 0
the next instruction in memory is : (null)

running - CCX:6 PC: 0
the next instruction in memory is : (null)

running - CCX:7 PC: 0
the next instruction in memory is : (null)

running - CCX:8 PC: 0
the next instruction in memory is : (null)

running - CCX:9 PC: 0
the next instruction in memory is : (null)

running - CCX:10 PC: 0
the next instruction in memory is : (null)

running - CCX:11 PC: 0
the next instruction in memory is : (null)
not null
not null
not null
not null
not null
not null
not null
not null
not null
not null
not null

在输出中可以看到,它实际上等于NULL,但是程序并不总是这样,而是在第二个print语句中打印“not null”,而不是NULL,就像它在第一个中一样。

of1yzvn4

of1yzvn41#

  • 有12个节点,从节点0到节点11。
  • 当到达最后一个节点时,当前节点被重置为-1,因此当前节点是下一个周期开始时的第0个(第一个)。
  • 程序检查当前节点的程序计数器是否需要重置。
  • 不存在这样的第-1个节点。

因此程序崩溃。

相关问题