-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_calloc.c
37 lines (34 loc) · 1.33 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
36
37
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: figarcia <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/03 13:05:45 by figarcia #+# #+# */
/* Updated: 2024/10/06 15:47:08 by figarcia ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t num_elements, size_t size_of_element)
{
size_t total_size;
size_t i;
void *ptr;
char *temp_ptr;
total_size = num_elements * size_of_element;
ptr = malloc(total_size);
temp_ptr = (char *)ptr;
i = 0;
if (num_elements > 0 && size_of_element > 0
&& total_size / num_elements != size_of_element)
return (NULL);
if (ptr == NULL)
return (NULL);
while (i < total_size)
{
temp_ptr[i] = 0;
i++;
}
return (ptr);
}