Hirohito Higashi wrote:

> > > 2018-2-9(Fri) 20:30:22 UTC+9 Bram Moolenaar:
> > > > Patch 8.0.1481
> > > > Problem:    Clearing a pointer takes two lines.
> > > > Solution:   Add vim_clear() to free and clear the pointer.
> > > > Files:      src/misc2.c, src/proto/misc2.pro, src/edit.c
> > > [...]
> > > 
> > > 
> > > In todo.txt
> > > > Use vim_clear() in more places, instead of vim_free() and assigning 
> > > > NULL.
> > > 
> > > I am working on the above items now.
> > > Wait about 6 hours.
> > > 
> > > Remarks:
> > > It includes the following changes.
> > > - Since cast is dangerous and troublesome, I changed vim_clear() from
> > > function to macro.
> > > 
> > > #define VIM_CLEAR(p) \
> > >     do { \
> > >         vim_free(p); \
> > >         p = NULL; \
> > >     } while (0)
> > 
> > I want to avoid using a macro, because the argument may not be a simple
> > pointer.
> 
> Hmm, at least my modified parts were all simple pointers.

As mentioned in the other message, it's logical that the pointer does
not have side effects.

> > Also, I intentionally only store NULL when it wasn't already set to NULL.
> > That should be faster.
> 
> Ah, I fixed the patch to check that it's not NULL.
> 
> > Why would the cast be dangerous?
> 
> Casting can trick compiler type checks. The thing that the compiler
> does not tell even if you pass the wrong argument is "dangerous".
> 
> For example, the following sample code will succeed in compiling. (on
> 64bit OS)
> 
> --------8<--------
> #include <stdlib.h>
> 
> void vim_clear(void **x)
> {
>     if (*x != NULL) {
>         free(*x);
>         *x = NULL;
>     }
> }
> 
> int main(void)
> {
>     long long a = 0;
> 
>     vim_clear((void **)a);
>     return 0;
> }
> --------8<--------

OK, it's a stupid mistake, but it's possible.

-- 
ARTHUR:  Well, I can't just call you `Man'.
DENNIS:  Well, you could say `Dennis'.
ARTHUR:  Well, I didn't know you were called `Dennis.'
DENNIS:  Well, you didn't bother to find out, did you?
                                  The Quest for the Holy Grail (Monty Python)

 /// Bram Moolenaar -- b...@moolenaar.net -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Raspunde prin e-mail lui