From f723389e5143c9ee0bc3d9774bb523b1c752b74c Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 8 Oct 2019 18:33:20 +0200 Subject: Fix ft_memmove and calloc - memmove doesnt depend on a buffer size - calloc return NULL if count or size == 0 - added byte type --- ft_calloc.c | 4 ++++ ft_memmove.c | 36 ++++++++++++++---------------------- libft.en.pdf | Bin 1455686 -> 0 bytes libft.h | 2 ++ pre2019_subject.en.pdf | Bin 0 -> 1455686 bytes 5 files changed, 20 insertions(+), 22 deletions(-) delete mode 100644 libft.en.pdf create mode 100644 pre2019_subject.en.pdf diff --git a/ft_calloc.c b/ft_calloc.c index a6d88a0..0cdce92 100644 --- a/ft_calloc.c +++ b/ft_calloc.c @@ -17,6 +17,10 @@ void *ft_calloc(size_t count, size_t size) { void *mem; + if (count == 0 || size == 0) + { + return (NULL); + } if ((mem = malloc(count * size)) == NULL) return (NULL); ft_bzero(mem, count * size); diff --git a/ft_memmove.c b/ft_memmove.c index 185fd36..d5d6789 100644 --- a/ft_memmove.c +++ b/ft_memmove.c @@ -12,32 +12,24 @@ #include #include - -#define BUF_SIZE (2 << 12) +#include "libft.h" void *ft_memmove(void *dst, const void *src, size_t len) { - size_t i; - size_t j; - size_t k; - unsigned char tmp[BUF_SIZE]; + size_t i; + t_byte *dst_cast; + t_byte *src_cast; if (dst == NULL && src == NULL) return (NULL); - i = 0; - while (i < len) - { - j = 0; - while (j < BUF_SIZE && i < len) - { - tmp[j] = ((unsigned char*)src)[i]; - j++; - i++; - } - k = -1; - while (++k < j) - ((unsigned char*)dst)[k] = tmp[k]; - i++; - } - return (dst); + dst_cast = (t_byte*)dst; + src_cast = (t_byte*)src; + i = -1; + if (dst_cast < src_cast) + while (++i < len) + dst_cast[i] = src_cast[i]; + else + while (len-- > 0) + dst_cast[len] = src_cast[len]; + return (dst); } diff --git a/libft.en.pdf b/libft.en.pdf deleted file mode 100644 index e5bee09..0000000 Binary files a/libft.en.pdf and /dev/null differ diff --git a/libft.h b/libft.h index 84cc8c1..6e1eb07 100644 --- a/libft.h +++ b/libft.h @@ -18,6 +18,8 @@ # define TRUE 1 # define FALSE 0 +typedef unsigned char t_byte; + typedef struct s_list { void *content; diff --git a/pre2019_subject.en.pdf b/pre2019_subject.en.pdf new file mode 100644 index 0000000..e5bee09 Binary files /dev/null and b/pre2019_subject.en.pdf differ -- cgit