Hi Bram,

2018-2-10(Sat) 21:10:17 UTC+9 Bram Moolenaar:
> 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.

> 
> 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<--------

--
Best regards,
Hirohito Higashi (h_east)

-- 
-- 
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