-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_calloc.c
More file actions
46 lines (43 loc) · 1.79 KB
/
ft_calloc.c
File metadata and controls
46 lines (43 loc) · 1.79 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nholbroo <nholbroo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 14:16:22 by nholbroo #+# #+# */
/* Updated: 2025/02/03 15:36:58 by nholbroo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
Which function:
Equivalent to the function "calloc" in stdlib.h.
Definition:
The calloc() function allocates memory for an array of nmemb elements
of size bytes each and returns a pointer to the allocated memory. The
memory is set to zero.
Return values:
If nmemb or size is 0, then calloc() returns
either NULL, or a unique pointer value that can later be successfully
passed to free().
If the multiplication of nmemb and size would result in integer overflow,
then calloc() returns an error.
@param nmemb How many elements.
@param size How many bytes is each element.
@param arr The allocated array to be returned.
*/
void *ft_calloc(size_t nmemb, size_t size)
{
unsigned char *arr;
size_t i;
i = 0;
if ((nmemb * size) > 2147483647)
return (NULL);
arr = (unsigned char *)malloc(nmemb * size);
if (arr == NULL)
return (NULL);
while (i < nmemb * size)
arr[i++] = 0;
return ((void *) arr);
}