接题目:
void *calloc(size_t nmemb, size_t size);
根据库文档:“函数calloc为一个数组分配内存,该数组有nmemb 个元素,每个元素为size字节。内存设置为0。如果nmemb或size为0,则calloc返回NULL。"
编写calloc的实现,通过调用malloc执行分配,调用memset将内存设置为0。你的代码应该没有任何由算术溢出引起的漏洞,且无论数据类型size_ t用多少位表示,代码都应该正常工作。
作为参考,函数malloc和memset声明如下:
void *malloc(size_t size);
void *memset(void *s ,int c ,size_t n);
开始作答 官方答案(已验证)
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
/* rename to avoid conflict */
void* another_calloc(size_t nmemb, size_t size) {
if (nmemb == 0 || size == 0) {
return NULL;
}
size_t buf_size = nmemb * size;
/* a good way to check overflow or not */
if (nmemb == buf_size / size) {
void* ptr = malloc(buf_size);
if (ptr != NULL) {
memset(ptr, 0, buf_size);
}
return ptr;
}
return NULL;
}
int main(int argc, char* argv[]) {
void* p;
p = another_calloc(0x1234, 1);
assert(p != NULL); free(p);
p = another_calloc(SIZE_MAX, 2);
assert(p == NULL);
free(p);
return 0;
}