ft_memcpy: do not try to copy to or from null
This commit is contained in:
parent
1d5123bd4a
commit
34a548bd78
1 changed files with 41 additions and 1 deletions
42
ft_memcpy.c
42
ft_memcpy.c
|
|
@ -6,16 +6,19 @@
|
|||
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/15 13:38:05 by kcolin #+# #+# */
|
||||
/* Updated: 2024/10/16 15:18:36 by kcolin ### ########.fr */
|
||||
/* Updated: 2024/10/22 12:03:47 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void *ft_memcpy(void *dest, const void *src, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if ((dest == NULL || src == NULL) && n > 0)
|
||||
return (dest);
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
|
|
@ -24,3 +27,40 @@ void *ft_memcpy(void *dest, const void *src, size_t n)
|
|||
}
|
||||
return (dest);
|
||||
}
|
||||
|
||||
/*
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "libft.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char dest[20] = "AAAAAAAAAAAAAAAAAAA";
|
||||
char src[20] = "Hello there";
|
||||
int i;
|
||||
|
||||
printf("%p\n", ft_memcpy(dest, src, 0));
|
||||
i = 0;
|
||||
while (i < 20)
|
||||
{
|
||||
printf("%d\t%c\n", dest[i], dest[i]);
|
||||
i++;
|
||||
}
|
||||
printf("%p\n", ft_memcpy(dest, src, 15));
|
||||
i = 0;
|
||||
while (i < 20)
|
||||
{
|
||||
printf("%d\t%c\n", dest[i], dest[i]);
|
||||
i++;
|
||||
}
|
||||
printf("%p\n", memcpy(dest, src, 15));
|
||||
i = 0;
|
||||
while (i < 20)
|
||||
{
|
||||
printf("%d\t%c\n", dest[i], dest[i]);
|
||||
i++;
|
||||
}
|
||||
printf("\n");
|
||||
printf("%p\n", ft_memcpy(NULL, NULL, 15));
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue