Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: (NULL == 0) ?



To answer the subject line, (NULL == 0) *is* guaranteed.  0 is the
representation of the null pointer constant in C.

>I would be willing to bet money it's a non-unix box.  Just think of
>the tremendous amount of unix code that uses the fact that calloc (in
>our case zcalloc) returns memory that is all zeros (and hence is
>NULL).  Since zsh only works on unix boxes, I think zeroing out memory
>with memset is ok.

A lot of C code assumes this, but it is broken.  No Unix standard
guarantees more than the C standard about the representation of null
pointers.

>I'm no expert on compilers, but I would be surprised if gcc could
>optimize such a loop to be as fast as memset.  Usually memset is
>hand optimized assembler code.

True, it generally won't be as good, but GCC does produce very
efficient code.  And the speed isn't really critical -- for example, in
this particular case, the code is only executed when you complete on a
command line with 16 words, and it then only needs to assign to 16
pointers.  (It could only cause a crash when you reach 32 words, and on
my system it took 64.)

>The function `newhashtable' uses zcalloc to allocate hashtable memory
>and the function `emptyhashtable' uses memset to set this same memory
>to zero when it is emptied.  If assuming NULL was equal to 0 would
>crash a machine, then these two pieces of code would have done it a
>long time ago.  The hashtable code gets executed a lot.

I can believe that zsh is not currently being used on any system where
the null pointer is not all-bits-zero.  Consequently it's not a really
important issue.  But code that makes this assumption is ill-formed.

-zefram




Messages sorted by: Reverse Date, Date, Thread, Author