-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_calloc.c
66 lines (58 loc) · 1.66 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: hecmarti <hecmarti@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/10 11:23:58 by hecmarti #+# #+# */
/* Updated: 2023/10/16 15:08:58 by hecmarti ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
void *ft_calloc(size_t count, size_t size)
{
size_t total_size;
void *ptr;
unsigned char *byte_ptr;
size_t i;
i = 0;
total_size = count * size;
ptr = malloc(total_size);
if (ptr != NULL)
{
byte_ptr = (unsigned char *)ptr;
while (i < total_size)
{
byte_ptr[i] = 0;
i++;
}
}
return (ptr);
}
/*
int main(void)
{
size_t num_elements = 5;
size_t element_size = sizeof(int);
int *arr = (int *)ft_calloc(num_elements, element_size);
if (arr != NULL)
{
printf("Se asignó memoria exitosamente.\n");
printf("Elementos del arreglo:\n");
for (size_t i = 0; i < num_elements; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
}
else
{
printf("Error al asignar memoria.\n");
}
return 0;
}
*/