How to check whether all bytes in a memory block are zero

后端 未结 10 840
渐次进展
渐次进展 2021-02-03 21:05

I have a block of memory with elements of fixed size, say 100 bytes, put into it one after another, all with the same fixed length, so memory looks like this

&l         


        
10条回答
  •  不要未来只要你来
    2021-02-03 21:55

    You could perhaps actually use memcmp without having to allocate a zero-valued array, like this:

    static int memvcmp(void *memory, unsigned char val, unsigned int size)
    {
        unsigned char *mm = (unsigned char*)memory;
        return (*mm == val) && memcmp(mm, mm + 1, size - 1) == 0;
    }
    

    The standard for memcmp does not say anything about overlapping memory regions.

提交回复
热议问题