== NULL, was Re: patch for GtkDialog



Charles Lambert <cwlambert76 yahoo com> writes:

> --- Joel Becker <jlbec evilplan org> wrote:
> > On Fri, Mar 29, 2002 at 04:22:23PM -0800, Charles
> > Lambert wrote:
> > > your test should be:
> > > if ( (NULL == rd) && (rd->response_id ==
> > > GTK_RESPONSE_CANCEL) )
> > > 
> > > NULL isn't guarenteed to be equal to zero
> > > (and yes i know i'm a perenthesis freak)
> > 
> > 	"if (rd)" is often considered to be idiomatic.  In
> > addition,
> > 0 is defined as the represention of the NULL
> > pointer.  ANSI C
> > specifically defines NULL as 0 or ((void *)0) or
> 
> the standard states that if ax is a pointer then (ax
> != 0) will convert zero to NULL
> 
> a direct quote from the ansi c99 standard states :
> 
> "NULL
> which expands to an implementaion-defined null pointer
> constant"
> 
> you cannot gaurentee that the complier will _always_
> use the zero memory address in all instances becuase
> iirc NULL is dictated by the operating system not the
> compiler. and gnome does run on multiple operating
> systems already.
> 
> i know that you, me or any other developer doesn't
> want to have to hunt down this bug when some compiler
> or OS decides to use some other value for NULL.

Really off-topic for gtk-devel-list, but to make sure everyone
is clear:

 * NULL is not guaranteed to be zero bits, but is identical
   to 0 cast to the appropriate pointer type, and 'if (rd)' 
   is guaranteed to work.

 * GTK+ actually requires NULL to be zero bits... it relies
   on g_new0() to initialize pointers to zero in various
   places. This is perfectly safe on any architecture we care 
   about running GTK+ on, but, yes, we know it is not 
   guaranteed to work by the C standard.

 * Stylistically Tim and I both follow the rule:

    * Use == NULL checks in g_return_if_fail() statements for
      maximum clarity in error messages shown to programmers.

    * Don't use == NULL elsewhere, its just excess noise.

   But I wouldn't say there is a hard and fast style rule for 
   code in GTK+ on this issue.

   Backwards comparisons (NULL == pointer) are definitely discouraged; 
   decent compilers warn on 'if (pointer = NULL) {}' without 
   this trick. 

Regards,
                                        Owen
   



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]