Difference between revisions of "CheckingNull"

From Nsnam
Jump to: navigation, search
 
 
Line 1: Line 1:
 
+
In general, when setting or checking a pointer to be a null value, you should explicitly use 0 rather than <code>NULL</code>.  To demonstrate, you should use:
In general, when setting or checking a pointer to be a null value, you should instead explicitly use 0 rather than <code>NULL</code>.  For example, you should use:
+
  
 
<code><pre>
 
<code><pre>

Latest revision as of 20:50, 23 June 2008

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.