Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This function increments the value pointed to by its argument. It also ensures that its argument is not a null pointer. But the pointer could still be invalid, causing the function to corrupt memory , or possibly terminate abnormally.

Code Block
bgColor#FFCCCC
void incr(int *intptr) {
  if (intptr == NULL) {
    /* handle error */
  }
  *intptr++;
}

...

By using the valid() function defined above, the function is less likely to dereference an invalid pointer or write to modify memory that is outside its the bounds of a valid object.

Code Block
bgColor#ccccff
void incr(int *intptr) {
  if (!valid(intptr)) {
    /* handle error */
  }
  *intptr++;
}

...

Because invalid pointers are often indicative of a bug defect in the program, one can use the assert() macro can be used to terminate immediately terminate if an invalid pointer is discovered (see MSC11-A. Incorporate diagnostic tests using assertions).

...