aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_calloc.c
blob: 7726c6480ab9b7f343958ee9eb04c7272ca7f244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "libft.h"
#include <stdlib.h>

void *
ft_calloc (size_t nmemb, size_t size)
{
  void *ptr;
  size_t total;

  // Detect multiplication overflow before allocating.
  if (nmemb && size > (size_t)-1 / nmemb)
    return (NULL);
  total = nmemb * size;
  ptr = malloc (total);
  if (!ptr)
    return (NULL);
  ft_bzero (ptr, total);
  return (ptr);
}