-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealloc.c
More file actions
44 lines (39 loc) · 839 Bytes
/
Copy pathrealloc.c
File metadata and controls
44 lines (39 loc) · 839 Bytes
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
#include "main.h"
/**
* _realloc - reallocates a memory block
* @ptr: pointer to the memory previously allocated with a call to malloc
* @old_size: size of ptr
* @new_size: size of the new memory to be allocated
*
* Return: pointer to the address of the new memory block
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
void *temp_block;
unsigned int i;
if (ptr == NULL)
{
temp_block = malloc(new_size);
return (temp_block);
}
else if (new_size == old_size)
return (ptr);
else if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
else
{
temp_block = malloc(new_size);
if (temp_block != NULL)
{
for (i = 0; i < min(old_size, new_size); i++)
*((char *)temp_block + i) = *((char *) ptr + i);
free(ptr);
return (temp_block);
}
else
return (NULL);
}
}