-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_calloc.c
More file actions
41 lines (37 loc) · 1.5 KB
/
ft_calloc.c
File metadata and controls
41 lines (37 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: antandre <antandre@student.42barcel> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 13:44:16 by antandre #+# #+# */
/* Updated: 2024/05/22 11:53:18 by antandre ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <stdlib.h>
#include "libft.h"
/*
* The function acts like malloc but with three error checks
* and initializes the bytes to 0.
* If count or size is 0, we allocate 1 byte of memory to return a
* non-null pointer that can be passed to free().
* If there is an integer overflow, it returns an error.
*/
void *ft_calloc(size_t count, size_t size)
{
void *vector;
vector = malloc(size * count);
if (vector == NULL)
return (NULL);
if (count == 0 || size == 0)
{
count = 1;
size = 1;
}
if (size != 0 && count > (size_t) - 1 / size)
return (NULL);
ft_bzero(vector, size * count);
return (vector);
}