Operation on … may be undefined?

前端 未结 3 1265
抹茶落季
抹茶落季 2021-02-02 06:21

I have the following code

FRAME frameArray[5][10]; // Create the array of frames
int trackBufferFull[5] = {0, 0, 0, 0, 0};// Keeps track of how full the buffer f         


        
3条回答
  •  日久生厌
    2021-02-02 06:49

    Line 77 is the line

    trackTail[nodeNumber-1] = ++trackTail[nodeNumber-1] % 10;
    

    You are changing trackTail[nodeNumber-1] twice between sequence points: once through ++, and once through assignment.

    This is undefined behaviour.

    The remedy is to rephrase the statement, for example like so:

    trackTail[nodeNumber-1] = (trackTail[nodeNumber-1] + 1) % 10;
    

    or like so:

    trackTail[nodeNumber-1]++;
    trackTail[nodeNumber-1] %= 10;
    

提交回复
热议问题