Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: rewrote a bit

...

Code Block
bgColor#FFCCCC
int insert(int index, int *list, int size, int value) {
  if (size != 0) {
    index = (index + 1) % size;
    list[index] = value;
    return index;
  }
  else {
    return -1;
  }
}

This noncompliant code example also violates INT01-C. Use rsize_t or size_t for all integer values representing the size of an object.

Compliant Solution (absolute value)

This compliant solution calculates the absolute value Taking the absolute value of the modulo operation returns a positive value.

Code Block
bgColor#ccccff#FFCCCC
int insert(int index, int *list, int size, int value) {
  if (size != 0) {
    index = abs((index + 1) % size);
    list[index] = value;
    return index;
  }
  else {
    return -1;
  }
}

However, this noncompliant code example violates INT01-C. Use rsize_t or size_t for all integer values representing the size of an object.

Compliant Solution (unsigned types)

However, the The most appropriate solution in this case is to use unsigned types to eliminate and possible implementation defined behavior, as in this compliant solution:

...