Skip to content

Commit

Permalink
adding 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfosse committed Mar 10, 2017
1 parent 71890b3 commit 5738cae
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 0x0B-more_malloc_free/2-calloc.c
Original file line number Diff line number Diff line change
@@ -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);
}
49 changes: 49 additions & 0 deletions 0x0B-more_malloc_free/2-main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "holberton.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/**
* 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);
}
Binary file added 0x0B-more_malloc_free/c
Binary file not shown.
1 change: 1 addition & 0 deletions 0x0B-more_malloc_free/holberton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5738cae

Please sign in to comment.