Ho il seguente codiceL'operazione su ... potrebbe essere indefinita?
FRAME frameArray[5][10]; // Create the array of frames
int trackBufferFull[5] = {0, 0, 0, 0, 0};// Keeps track of how full the buffer for each node is
int trackFront[5] = {0, 0, 0, 0, 0}; // Array to keep track of which is the front of the array
int trackTail[5] = {0, 0, 0, 0, 0};
// Function to add to the array (CHANGE int frame)
void addFrame (int nodeNumber, FRAME frame)
{
//Calc tail
int tail = trackTail[nodeNumber-1];
// Calc frames in buffer
int framesinBuffer = trackBufferFull[nodeNumber-1];
if (framesinBuffer == 10)
{
printf("Buffer is full\n");
}
else
{
// Add frame to frameArray
frameArray[nodeNumber-1][tail] = frame;
printf("\nAdded a frame in node: %i to the buffer\n", nodeNumber);
// Increment the count
trackBufferFull[nodeNumber-1]++;
trackTail[nodeNumber-1] = ++trackTail[nodeNumber-1] % 10;
}
}
Gli array che uso per frameArray è un avvolgente/array ciclico di lunghezza 10, quindi, perché ho il codice
trackTail[nodeNumber-1] = ++trackTail[nodeNumber-1] % 10;
Tutto funziona perfettamente in un file autonomo, tuttavia quando eseguito all'interno di un file di dimensioni maggiori, ottengo i seguenti errori di compilazione:
$ cnet GARETH -m 30
compiling gareth.c
gareth.c: In function ‘addFrame’:
gareth.c:77:27: error: operation on ‘trackTail[nodeNumber + -0x00000000000000001]’ may be undefined [-Werror=sequence-point]
gareth.c: In function ‘removeFirstFrame’:
gareth.c:98:28: error: operation on ‘trackFront[nodeNumber + -0x00000000000000001]’ may be undefined [-Werror=sequence-point]
gareth.c:105:1: error: control reaches end of non-void function [-Werror=return-type]
cc1: all warnings being treated as errors
linea 77 è la linea
trackTail[nodeNumber-1] = ++trackTail[nodeNumber-1] % 10;
Help.
per vedere il codice con numeri di riga e il lato gli errori a fianco, ho caricato un'immagine: http://i.imgur.com/wyO5a.png
http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points - you're _lucky_ il compilatore ti avvisa. – Mat
Una domanda che si pone raramente. +1. –