Why does calloc require two parameters and malloc just one?

前端 未结 5 1895
礼貌的吻别
礼貌的吻别 2020-12-09 09:39

It\'s very bothersome for me to write calloc(1, sizeof(MyStruct)) all the time. I don\'t want to use an idea like wrapping this method and etc. I mean I want to

5条回答
  •  臣服心动
    2020-12-09 10:12

    it is just by design.

    you could write your own calloc

    void *mycalloc(size_t num, size_t size)
    {
        void *block = malloc(num * size);
        if(block != NULL)
            memset(block, 0, num * size);
        return block;
    }
    

提交回复
热议问题