-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_calloc.c
35 lines (32 loc) · 1.25 KB
/
ft_calloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edesaint <edesaint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/13 18:18:49 by edesaint #+# #+# */
/* Updated: 2022/12/06 19:53:41 by edesaint ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdint.h>
#include "libft.h"
void *ft_calloc(size_t nmemb, size_t size)
{
void *ptr;
if (nmemb == 0 || size == 0)
{
ptr = malloc(1);
if (!ptr)
return (NULL);
((unsigned char *)ptr)[0] = 0;
return (ptr);
}
if (nmemb > SIZE_MAX / size)
return (NULL);
ptr = (void *) malloc(nmemb * size);
if (!ptr)
return (NULL);
ft_bzero(ptr, nmemb * size);
return (ptr);
}