diff --git a/0x0B-more_malloc_free/0-main.c b/0x0B-more_malloc_free/0-main.c new file mode 100644 index 0000000..fca2eec --- /dev/null +++ b/0x0B-more_malloc_free/0-main.c @@ -0,0 +1,31 @@ +#include "holberton.h" +#include +#include +#include + +/** + * main - check the code for Holberton School students. + * + * Return: Always 0. + */ +int main(void) +{ + char *c; + int *i; + float *f; + double *d; + + c = malloc_checked(sizeof(char) * 1024); + printf("%p\n", (void *)c); + i = malloc_checked(sizeof(int) * 402); + printf("%p\n", (void *)i); + f = malloc_checked(sizeof(float) * 100000000); + printf("%p\n", (void *)f); + d = malloc_checked(INT_MAX); + printf("%p\n", (void *)d); + free(c); + free(i); + free(f); + free(d); + return (0); +} diff --git a/0x0B-more_malloc_free/0-malloc_checked.c b/0x0B-more_malloc_free/0-malloc_checked.c new file mode 100644 index 0000000..e745730 --- /dev/null +++ b/0x0B-more_malloc_free/0-malloc_checked.c @@ -0,0 +1,17 @@ +#include "holberton.h" +/** + * malloc_checked - alloctes memory using malloc. + * @b: size. + * Return: returns pointer, otherwise 98 on fail. + */ +void *malloc_checked(unsigned int b) +{ + void *temp; + + temp = malloc(b); + if (temp == NULL) + { + exit(98); + } + return (temp); +} diff --git a/0x0B-more_malloc_free/README.md b/0x0B-more_malloc_free/README.md new file mode 100644 index 0000000..5f8f7af --- /dev/null +++ b/0x0B-more_malloc_free/README.md @@ -0,0 +1,3 @@ +0x0B-more_malloc_free + +add content here... diff --git a/0x0B-more_malloc_free/_putchar.c b/0x0B-more_malloc_free/_putchar.c new file mode 100644 index 0000000..713bea1 --- /dev/null +++ b/0x0B-more_malloc_free/_putchar.c @@ -0,0 +1,13 @@ +#include + +/** + * * _putchar - writes the character c to stdout + * * @c: The character to print + * * + * * Return: On success 1. + * * On error, -1 is returned, and errno is set appropriately. + * */ +int _putchar(char c) +{ + return (write(1, &c, 1)); +} diff --git a/0x0B-more_malloc_free/a b/0x0B-more_malloc_free/a new file mode 100755 index 0000000..f8b43c4 Binary files /dev/null and b/0x0B-more_malloc_free/a differ diff --git a/0x0B-more_malloc_free/holberton.h b/0x0B-more_malloc_free/holberton.h new file mode 100644 index 0000000..0b0248c --- /dev/null +++ b/0x0B-more_malloc_free/holberton.h @@ -0,0 +1,10 @@ +#ifndef HOLBERTON +#define HOLBERTON + +#include +#include + +void *malloc_checked(unsigned int b); +int _putchar(char c); + +#endif