From c795a9bab1157d4d9b2b2c28aa082623e5ec2850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 17 Oct 2024 13:53:44 +0200 Subject: [PATCH] ft_calloc: correctly set allocated memory to zero --- ft_calloc.c | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/ft_calloc.c b/ft_calloc.c index 2d49227..49a29a1 100644 --- a/ft_calloc.c +++ b/ft_calloc.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 10:20:05 by kcolin #+# #+# */ -/* Updated: 2024/10/17 10:40:21 by kcolin ### ########.fr */ +/* Updated: 2024/10/17 13:50:18 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,31 +16,37 @@ void *ft_calloc(size_t nmemb, size_t size) { size_t bytes; + void *out; bytes = nmemb * size; if (nmemb != 0 && bytes / nmemb != size) return (NULL); else - return (malloc(bytes)); + { + out = malloc(bytes); + ft_memset(out, 0, bytes); + return (out); + } } -/* -int main(void) -{ - free(ft_calloc(0, sizeof(int))); - free(calloc(0, sizeof(int))); - printf("%p\t%p\n", - ft_calloc(0, sizeof(int)), - calloc(0, sizeof(int))); - free(ft_calloc(1024, sizeof(int))); - free(calloc(1024, sizeof(int))); - printf("%p\t%p\n", - ft_calloc(1024, sizeof(int)), - calloc(1024, sizeof(int))); - free(ft_calloc(SIZE_MAX, sizeof(int))); - free(calloc(SIZE_MAX, sizeof(int))); - printf("%p\t%p\n", - ft_calloc(SIZE_MAX, sizeof(int)), - calloc(SIZE_MAX, sizeof(int))); -} -*/ +/* #include */ +/* #include */ + +/* int main(void) */ +/* { */ +/* free(ft_calloc(0, sizeof(int))); */ +/* free(calloc(0, sizeof(int))); */ +/* printf("%p\t%p\n", */ +/* ft_calloc(0, sizeof(int)), */ +/* calloc(0, sizeof(int))); */ +/* free(ft_calloc(1024, sizeof(int))); */ +/* free(calloc(1024, sizeof(int))); */ +/* printf("%p\t%p\n", */ +/* ft_calloc(1024, sizeof(int)), */ +/* calloc(1024, sizeof(int))); */ +/* free(ft_calloc(SIZE_MAX, sizeof(int))); */ +/* free(calloc(SIZE_MAX, sizeof(int))); */ +/* printf("%p\t%p\n", */ +/* ft_calloc(SIZE_MAX, sizeof(int)), */ +/* calloc(SIZE_MAX, sizeof(int))); */ +/* } */