Libft
42 Libft library documentation
Loading...
Searching...
No Matches
ft_calloc.c File Reference
+ Include dependency graph for ft_calloc.c:

Go to the source code of this file.

Functions

void * ft_calloc (size_t nmemb, size_t size)
 Allocates and zero-initializes an array.
 

Function Documentation

◆ ft_calloc()

void * ft_calloc ( size_t  nmemb,
size_t  size 
)

Allocates and zero-initializes an array.

Allocates memory for nmemb elements of size bytes each and sets all allocated bytes to zero. Returns NULL on overflow or allocation failure.

Parameters
nmembNumber of elements.
sizeSize of each element in bytes.
Returns
Pointer to allocated memory, or NULL on failure.

Definition at line 26 of file ft_calloc.c.

27{
28 void *ptr;
29 size_t total_size;
30
31 if (nmemb == 0 || size == 0)
32 return (malloc(0));
33 if (nmemb > (size_t)-1 / size)
34 return (NULL);
35 total_size = nmemb * size;
36 ptr = malloc(total_size);
37 if (!ptr)
38 return (NULL);
39 ft_memset(ptr, 0, total_size);
40 return (ptr);
41}
void * ft_memset(void *s, int c, size_t len)
Fills a memory area with a byte value.
Definition ft_memset.c:23

References ft_memset().

+ Here is the call graph for this function: