-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_calloc.c
More file actions
64 lines (57 loc) · 1.96 KB
/
ft_calloc.c
File metadata and controls
64 lines (57 loc) · 1.96 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simarcha <simarcha@student.42barcelona. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 19:27:38 by simarcha #+# #+# */
/* Updated: 2024/09/30 13:23:53 by simarcha ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
//you have to fulfill your memory from malloc with 0
//step 0: check if everything is all right
//step 1: do malloc
//step 2: fulfill it with NULL characters
//return the malloc argument
//the first argument is the total length of the memory
//the second argument is the size of each cells in this memory
// https://manpages.ubuntu.com/manpages/focal/man3/malloc.3.html
// NULL may also be returned by a successful call to malloc() with a size of
//zero, or by a successful call to calloc() with count or size equal to zero.
void *ft_calloc(size_t count, size_t size)
{
char *str;
size_t i;
i = 0;
if (count * size == 0)
return (NULL);
str = malloc(count * size);
if (!str)
return (NULL);
while (i < count)
{
str[i] = 0;
i++;
}
return (str);
}
/*#include <stdlib.h>
#include <stdio.h>
int main(void)
{
size_t count;
size_t size;
unsigned char *res;
unsigned char *array;
count = 30;
size = 1;
res = calloc(count, size);
printf("Lib : __%s__\n", res);
free(res);
array = ft_calloc(count, size);
printf("Sim : __%s__\n", array);
free(array);
return (0);
}*/