From a38e9e5f943ce22101d356119f5cf30053684f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 17 Oct 2024 10:42:43 +0200 Subject: [PATCH] ft_calloc: initial implementation --- Makefile | 5 +++-- ft_calloc.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ libft.h | 4 +++- 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 ft_calloc.c diff --git a/Makefile b/Makefile index 47270de..b586977 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: kcolin +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/14 13:43:59 by kcolin #+# #+# # -# Updated: 2024/10/16 16:02:21 by kcolin ### ########.fr # +# Updated: 2024/10/17 10:21:34 by kcolin ### ########.fr # # # # **************************************************************************** # @@ -32,7 +32,8 @@ SOURCES = ft_isalpha.c \ ft_memchr.c \ ft_memcmp.c \ ft_strnstr.c \ - ft_atoi.c + ft_atoi.c \ + ft_calloc.c OBJECTS = $(SOURCES:.c=.o) CC = gcc diff --git a/ft_calloc.c b/ft_calloc.c new file mode 100644 index 0000000..2d49227 --- /dev/null +++ b/ft_calloc.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 10:20:05 by kcolin #+# #+# */ +/* Updated: 2024/10/17 10:40:21 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void *ft_calloc(size_t nmemb, size_t size) +{ + size_t bytes; + + bytes = nmemb * size; + if (nmemb != 0 && bytes / nmemb != size) + return (NULL); + else + return (malloc(bytes)); +} + +/* +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))); +} +*/ diff --git a/libft.h b/libft.h index a6c1987..4acdc95 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 10:11:54 by kcolin #+# #+# */ -/* Updated: 2024/10/16 16:06:06 by kcolin ### ########.fr */ +/* Updated: 2024/10/17 10:19:51 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,4 +44,6 @@ char *ft_strnstr(const char *big, const char *little, size_t len); int ft_atoi(const char *nptr); +void *ft_calloc(size_t nmemb, size_t size); + #endif