开发者

aligned calloc, visual studio

There is a function called _aligned_malloc which can alloca开发者_运维技巧te aligned memory. Is there a similar function for calloc? I want to align and initialize it to zero. I am using visual studio 2010


No, but you can roll your own pretty easily:

void *_aligned_calloc(size_t nelem, size_t elsize, size_t alignment)
{
    // Watch out for overflow
    if(elsize == 0 || nelem >= SIZE_T_MAX/elsize)
        return NULL;

    size_t size = nelem * elsize;
    void *memory = _aligned_malloc(size, alignment);
    if(memory != NULL)
        memset(memory, 0, size);
    return memory;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜