CheckingNull

From Nsnam
Revision as of 20:50, 23 June 2008 by Tjkopena (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In general, when setting or checking a pointer to be a null value, you should explicitly use 0 rather than NULL. To demonstrate, you should use:

  if (ptr == 0) {
    // code failed, whine to listserv
  }

As opposed to the commonly used:

  if (ptr == NULL) {
    // code failed, whine to listserv but say you used 0
  }

In any event, you should definitely not use:

  if (!ptr) {
    // you're fired!
  }

There is some discussion about this issue here. In general, NULL is not as crisply defined or its use as straightforwardly intuitive as using 0. The implicit check is also generally avoided in NS3 for reasons of code clarity.