diff --git a/0x0B-more_malloc_free/2-calloc.c b/0x0B-more_malloc_free/2-calloc.c new file mode 100644 index 0000000..2e8574c --- /dev/null +++ b/0x0B-more_malloc_free/2-calloc.c @@ -0,0 +1,25 @@ +#include "holberton.h" + +/** + * _calloc - new calloc using malloc + * @nmemb: number of elements. + * @size: size of mem. + * Return: returns pointer, NULL on fail. + */ +void *_calloc(unsigned int nmemb, unsigned int size) +{ + char *temp; + unsigned int i; + + if (nmemb == 0 || size == 0) + return (NULL); + + temp = malloc(nmemb * size); + if (temp == NULL) + return (NULL); + + for (i = 0; i < (nmemb * size); i++) + temp[i] = 0; + + return (temp); +} diff --git a/0x0B-more_malloc_free/2-main.c b/0x0B-more_malloc_free/2-main.c new file mode 100644 index 0000000..214532d --- /dev/null +++ b/0x0B-more_malloc_free/2-main.c @@ -0,0 +1,49 @@ +#include "holberton.h" +#include +#include +#include + +/** + * simple_print_buffer - prints buffer in hexa + * @buffer: the address of memory to print + * @size: the size of the memory to print + * + * Return: Nothing. + */ +void simple_print_buffer(char *buffer, unsigned int size) +{ + unsigned int i; + + i = 0; + while (i < size) + { + if (i % 10) + { + printf(" "); + } + if (!(i % 10) && i) + { + printf("\n"); + } + printf("0x%02x", buffer[i]); + i++; + } + printf("\n"); +} + +/** + * main - check the code for Holberton School students. + * + * Return: Always 0. + */ +int main(void) +{ + char *a; + + a = _calloc(98, sizeof(char)); + strcpy(a, "Holberton"); + strcpy(a + 9, " School! :)\n"); + a[97] = '!'; + simple_print_buffer(a, 98); + return (0); +} diff --git a/0x0B-more_malloc_free/c b/0x0B-more_malloc_free/c new file mode 100755 index 0000000..8658ff5 Binary files /dev/null and b/0x0B-more_malloc_free/c differ diff --git a/0x0B-more_malloc_free/holberton.h b/0x0B-more_malloc_free/holberton.h index ee82061..e19109e 100644 --- a/0x0B-more_malloc_free/holberton.h +++ b/0x0B-more_malloc_free/holberton.h @@ -7,5 +7,6 @@ void *malloc_checked(unsigned int b); int _putchar(char c); char *string_nconcat(char *s1, char *s2, unsigned int n); +void *_calloc(unsigned int nmemb, unsigned int size); #endif