1、问题
编写calloc函数,函数内部使用malloc函数来获取内存
2、代码实现
#include <stdio.h>
#include <stdlib.h>void *my_calloc(size_t nums, size_t element_size)
{int i, all_size;all_size = nums * element_size;char *p = NULL;p = malloc(all_size);if (p == NULL)return NULL;for (i = 0; i < all_size; ++i)p[i] = 0;return p;
}int main()
{char *p = my_calloc(10, sizeof(char));if (p != NULL){for (int i = 0; i < 10; ++i){printf("p[%d] is %d\n", i, p[i]);}free(p);}int *p_int = my_calloc(10, sizeof(int));if (p_int != NULL){for (int i = 0; i < 10; ++i)