Skip to content

Commit 5a00708

Browse files
committed
task 2
1 parent dc1388d commit 5a00708

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

0x0C-more_malloc_free/2-calloc.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "main.h"
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
/**
5+
* _calloc - calloc function
6+
* @nmemb: number of elements
7+
* @size: size of bytes
8+
* Return: pointer or void
9+
*/
10+
void *_calloc(unsigned int nmemb, unsigned int size)
11+
{
12+
char *t;
13+
unsigned int i;
14+
15+
if (nmemb == 0 || size == 0)
16+
return (NULL);
17+
t = malloc(nmemb * size);
18+
if (t == NULL)
19+
return (NULL);
20+
for (i = 0; i < nmemb * size; i++)
21+
t[i] = 0;
22+
23+
return (t);
24+
}

0x0C-more_malloc_free/main.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ void *malloc_checked(unsigned int b);
304304

305305
char *string_nconcat(char *s1, char *s2, unsigned int n);
306306

307+
/**
308+
* _calloc - allocates memory for array using malloc
309+
*/
310+
311+
void *_calloc(unsigned int nmemb, unsigned int size);
307312

308313
#endif
309314

0 commit comments

Comments
 (0)