diff options
Diffstat (limited to '2/homework/implement_calloc.c')
| -rw-r--r-- | 2/homework/implement_calloc.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/2/homework/implement_calloc.c b/2/homework/implement_calloc.c new file mode 100644 index 0000000..62c3804 --- /dev/null +++ b/2/homework/implement_calloc.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +void *my_calloc(size_t nmemb, size_t size) { + if (nmemb == 0 || size == 0) { + return NULL; + } + /* Check if invariant of multiplication is broken by overflow */ + size_t membytes = nmemb * size; + if (membytes/nmemb != size) { + return NULL; + } + void *mem_or_null = malloc(membytes); + if (mem_or_null == NULL) { + return NULL; + } + + return memset(mem_or_null, 0, membytes); +} + +int main() { + char *buf = my_calloc(10, 15); + int i; + for (i=0; i<10*15; ++i) + printf("%c", 97 + buf[i]); + return 0; +} |
