-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_calloc.c
66 lines (58 loc) · 2.3 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hicunha- <hicunha-@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/04 09:38:11 by hicunha- #+# #+# */
/* Updated: 2023/10/04 09:38:11 by hicunha- ### ########.fr */
/* */
/* ************************************************************************** */
/*
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.
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.
The malloc() and calloc() functions return a pointer to the allocated memory,
which is suitably aligned for any built-in type. On error, these functions
return NULL.
NULL may also be returned by a successful call to malloc() with a size of zero,
or by a successful call to calloc() with nmemb or size equal to zero.
*/
#include "libft.h"
void *ft_calloc(size_t nmemb, size_t size)
{
unsigned char *dest;
size_t i;
i = 0;
dest = malloc(nmemb * size);
if (dest == NULL)
return (NULL);
while (i < (nmemb * size))
{
dest[i] = 0;
i++;
}
return (dest);
}
/*#include <stdio.h>
int main()
{
size_t num_elements = 5;
size_t element_size = sizeof(int);
int *array = (int *)ft_calloc(num_elements, element_size);
if (array == NULL)
{
printf("A alocação de memória falhou.\n");
return (1);
}
printf("Memória alocada e inicializada com sucesso:\n");
for (size_t i = 0; i < num_elements; ++i)
printf("%d ", array[i]);
free(array);
return (0);
}*/