-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100-realloc.c
50 lines (42 loc) · 1.09 KB
/
100-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
46
47
48
49
50
#include "main.h"
#include <stdlib.h>
/**
* _realloc - reallocates a memory block using malloc and free
* @ptr: pointer to the memory previously allocated with a call to malloc
* @old_size: size, in bytes, of the allocated space for ptr
* @new_size: new size, in bytes of the new memory block
*
* Return: pointer to the new memory block
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
char *result;
unsigned int i;
/* if new_size == old_size, do nothing and return ptr */
if (new_size == old_size)
return (ptr);
/* if new_size == 0 and ptr is not NULL, call free(ptr) */
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
/* if ptr is NULL, call malloc(new_size) */
if (ptr == NULL)
{
result = malloc(new_size);
if (result == NULL)
return (NULL);
return (result);
}
/* allocate memory for new_size */
result = malloc(new_size);
if (result == NULL)
return (NULL);
/* copy contents of ptr to result */
for (i = 0; i < old_size && i < new_size; i++)
result[i] = ((char *)ptr)[i];
/* free ptr */
free(ptr);
return (result);
}