-
Notifications
You must be signed in to change notification settings - Fork 1
/
_realloc.c
45 lines (44 loc) · 891 Bytes
/
_realloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "main.h"
/**
* _realloc - reeallocates a memory block using malloc and free
* @ptr: pointer to reallocate memory of
* @old_size: the old size of the pointer
* @new_size: the new size of the pointer
* Return: the reassigned pointer (Success)
* NULL (Failure) when malloc fails to assign or new_size is 0
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
char *real = NULL, *temp;
unsigned int i;
if (new_size == old_size)
return (ptr);
if (!ptr)
{
return (malloc(new_size));
}
if (new_size == 0 && ptr)
{
free(ptr);
return (NULL);
}
temp = ptr;
real = malloc(new_size);
if (!real)
{
free(ptr);
return (NULL);
}
if (new_size < old_size)
{
for (i = 0; i < new_size; i++)
real[i] = temp[i];
}
else if (new_size > old_size)
{
for (i = 0; i < old_size; i++)
real[i] = temp[i];
}
free(ptr);
return (real);
}