Hacker News new | comments | show | ask | jobs | submit login

Personally, I'd just write a wrapper function for memset().

The following one works in at least gcc 4.5.3 and clang 3.1, but is actually not guaranteed to work by C language semantics:

    static inline void memzero(void *volatile block, size_t size)
    {
        memset(block, 0, size);
    }
A safe alternative is

    static inline void memzero(void *block, size_t size)
    {
        static void *(*volatile const memset_)(void *, int, size_t) = memset;
        memset_(block, 0, size);
    }
but has the downside that the virtual call cannot be optimized away, whereas gcc and clang actually inline the call to memset() if the first version is used.

If this is a concern, there's probably no alternative to an explicit loop:

    static inline void memzero(void *block, size_t size)
    {
        volatile unsigned char *bp = block;
        while(bp < (unsigned char *)block + size)
            *bp++ = 0;
    }



Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: