-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_calloc.c
57 lines (52 loc) · 1.63 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: geonwkim <geonwkim@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/19 13:07:12 by geonwkim #+# #+# */
/* Updated: 2024/04/20 01:45:27 by geonwkim ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t n_items, size_t size)
{
void *p;
size_t size_bytes;
if (n_items == 0 || size == 0)
{
n_items = 1;
size = 1;
}
if (n_items > SIZE_MAX / size)
return (NULL);
size_bytes = n_items * size;
p = malloc(size_bytes);
if (p != NULL)
ft_memset(p, 0, size_bytes);
if (p == NULL)
return (NULL);
return (p);
}
// int main()
// {
// // Check if memory allocation was successful
// int* array = (int*)ft_calloc(5, sizeof(int));
// int i;
// i = 0;
// if (array)
// {
// while (i < 5)
// {
// // This should print "0 0 0 0 0 "
// printf("%d ", array[i]);
// i++;
// }
// printf("\n");
// free(array);
// }
// else
// printf("Memory allocation failed\n");
// return (0);
// }