calloc and strdup

This commit is contained in:
David Gailleton
2025-11-05 19:37:02 +01:00
parent 697e044d7e
commit 86f9f05889
4 changed files with 60 additions and 2 deletions

View File

@@ -30,7 +30,9 @@ SRC = ft_isalpha.c \
ft_memcpy.c \ ft_memcpy.c \
ft_memchr.c \ ft_memchr.c \
ft_strnstr.c \ ft_strnstr.c \
ft_atoi.c ft_atoi.c \
ft_calloc.c \
ft_strdup.c
OBJ_DIR = objs OBJ_DIR = objs

24
ft_calloc.c Normal file
View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 19:26:10 by dgaillet #+# #+# */
/* Updated: 2025/11/05 19:29:48 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *calloc(size_t nmemb, size_t size)
{
void *all_mem;
all_mem = malloc(size * nmemb);
if (!all_mem)
return (NULL);
ft_bzero(all_mem, nmemb * size);
return (all_mem);
}

28
ft_strdup.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 19:31:29 by dgaillet #+# #+# */
/* Updated: 2025/11/05 19:35:13 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *s)
{
char *dest;
int i;
dest = malloc(sizeof(char) *(ft_strlen(s) + 1));
if (!dest)
return (NULL);
i = 0;
while (s[i++])
dest[i] = s[i];
dest[i] = '\0';
return (dest);
}

View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 09:49:43 by dgaillet #+# #+# */ /* Created: 2025/11/05 09:49:43 by dgaillet #+# #+# */
/* Updated: 2025/11/05 19:06:38 by dgaillet ### ########lyon.fr */ /* Updated: 2025/11/05 19:36:02 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -34,5 +34,9 @@ char *ft_strrchr(const char *s, int c);
int ft_strncmp(const char *s1, const char *s2, size_t n); int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n); int ft_memcmp(const void *s1, const void *s2, size_t n);
void *ft_memchr(const void *s, int c, size_t n); void *ft_memchr(const void *s, int c, size_t n);
char *ft_strnstr(const char *big, const char *little, size_t len);
int ft_atoi(const char *nptr);
void *calloc(size_t nmemb, size_t size);
char *ft_strdup(const char *s);
#endif #endif