-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_calloc.c
36 lines (34 loc) · 1.56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oostapen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 21:20:24 by oostapen #+# #+# */
/* Updated: 2024/04/15 14:53:28 by oostapen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* Function: ft_calloc (short for "contiguous allocation")
one after another, without gaps
* ------------------------------------------------------
* Allocates memory for an array of 'count' elements,
* each of 'size' bytes, and initializes to zero.
* Checks for overflow in 'count' * 'size'.
* If overflow or allocation fails, returns NULL.
* Uses ft_memset to set allocated memory to zero.
* Returns a pointer to the allocated memory.
*/
void *ft_calloc(size_t count, size_t size)
{
unsigned char *tmp;
if (count > 0 && SIZE_MAX / count < size)
return (NULL);
tmp = malloc(count * size);
if (!tmp)
return (NULL);
ft_memset(tmp, 0, count * size);
return (tmp);
}