From 365e8abb428e9b1198c13a5d56e6c73cead6159a Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Thu, 28 Nov 2019 16:01:08 +0100 Subject: [PATCH] resolu calloc pour taille zero --- srcd/part1/ft_calloc.c | 35 +++++++++++++++++++++++++++++++++-- srcs/ft_calloc.c | 14 +------------- srcs/ft_strlcpy.c | 12 ------------ 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/srcd/part1/ft_calloc.c b/srcd/part1/ft_calloc.c index 611ce62..b8ef390 100644 --- a/srcd/part1/ft_calloc.c +++ b/srcd/part1/ft_calloc.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/25 13:54:53 by hulamy #+# #+# */ -/* Updated: 2019/11/25 14:28:35 by hulamy ### ########.fr */ +/* Updated: 2019/11/28 16:00:32 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,13 +20,44 @@ ** return a pointer to the allocated memory */ +/* +** #include +** +** void ft_bzero(void *s, size_t n) +** { +** size_t i; +** unsigned char *ptr; +** +** if (n) +** { +** ptr = (unsigned char *)s; +** i = 0; +** while (i < n) +** ptr[i++] = '\0'; +** } +** } +** +** void *ft_calloc(size_t count, size_t size); +** +** int main(void) +** { +** void *str; +** +** str = ft_calloc(0, 0); +** if (str == ((void *)0)) +** printf("failed\n"); +** free(str); +** return (0); +** } +*/ + #include "libft.h" void *ft_calloc(size_t count, size_t size) { void *tmp; - if (!count || !size || !(tmp = malloc(count * size))) + if (!(tmp = malloc(count * size))) return (NULL); ft_bzero(tmp, count * size); return (tmp); diff --git a/srcs/ft_calloc.c b/srcs/ft_calloc.c index 611ce62..4848191 100644 --- a/srcs/ft_calloc.c +++ b/srcs/ft_calloc.c @@ -1,15 +1,3 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_calloc.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/25 13:54:53 by hulamy #+# #+# */ -/* Updated: 2019/11/25 14:28:35 by hulamy ### ########.fr */ -/* */ -/* ************************************************************************** */ - /* ** exemple allocation for 5 integers with malloc then calloc : ** a = (int *)malloc(5 * sizeof(int)); //5*4bytes = 20 bytes @@ -26,7 +14,7 @@ void *ft_calloc(size_t count, size_t size) { void *tmp; - if (!count || !size || !(tmp = malloc(count * size))) + if (!(tmp = malloc(count * size))) return (NULL); ft_bzero(tmp, count * size); return (tmp); diff --git a/srcs/ft_strlcpy.c b/srcs/ft_strlcpy.c index 9b33a47..db57b07 100644 --- a/srcs/ft_strlcpy.c +++ b/srcs/ft_strlcpy.c @@ -1,15 +1,3 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strlcpy.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/25 13:57:19 by hulamy #+# #+# */ -/* Updated: 2019/11/25 15:53:19 by hulamy ### ########.fr */ -/* */ -/* ************************************************************************** */ - /* ** copy size - 1 length of src into dest, ** terminate it with a '\0'