Skip to content

Commit 2d4e8c4

Browse files
committed
Submit solution
1 parent a437eb3 commit 2d4e8c4

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

0x0C-more_malloc_free/100-realloc.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "main.h"
2+
3+
4+
/**
5+
* _realloc - reallocates a memory block
6+
* @ptr: pointer to the memory previously allocated with a call to malloc
7+
* @old_size: size of ptr
8+
* @new_size: size of the new memory to be allocated
9+
*
10+
* Return: pointer to the address of the new memory block
11+
*/
12+
13+
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
14+
{
15+
void *temp_block;
16+
unsigned int i;
17+
18+
if (ptr == NULL)
19+
{
20+
temp_block = malloc(new_size);
21+
return (temp_block);
22+
}
23+
else if (new_size == old_size)
24+
return (ptr);
25+
26+
else if (new_size == 0 && ptr != NULL)
27+
{
28+
free(ptr);
29+
return (NULL);
30+
}
31+
else
32+
{
33+
temp_block = malloc(new_size);
34+
if (temp_block != NULL)
35+
{
36+
for (i = 0; i < min(old_size, new_size); i++)
37+
*((char *)temp_block + i) = *((char *) ptr + i);
38+
free(ptr);
39+
return (temp_block);
40+
}
41+
else
42+
return (NULL);
43+
}
44+
}

0 commit comments

Comments
 (0)