-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealloc.c
56 lines (53 loc) · 1.11 KB
/
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
51
52
53
54
55
56
#include "main.h"
/**
* _memcpy - copies n bytes from memory area src to memory area dest
* @src: memory area to copy from
* @dest: memory area to copy to
* @n: number of bytes to be copied
* Return: memory copied to
*/
void *_memcpy(void *dest, const void *src, size_t n)
{
size_t i;
char *dst_ptr = dest;
const char *src_ptr = src;
for (i = 0; i < n; i++)
dst_ptr[i] = src_ptr[i];
return (dest);
}
/**
* _realloc - works just like the standard library c realloc
* @ptr: pinter to be reallocated
* @old_size: old allocated size
* @new_size: new allocated size
* Return: pointer to allocated memory
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
void *tmp;
if (new_size == old_size)
return (ptr);
if (ptr == NULL)
{
/*free(ptr);*/
ptr = malloc(new_size);
if (ptr == NULL)
return (NULL);
return (ptr);
}
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
tmp = malloc(new_size);
if (tmp == NULL)
return (NULL);
if (old_size < new_size)
_memcpy(tmp, ptr, old_size);
else
_memcpy(tmp, ptr, new_size);
free(ptr);
ptr = tmp;
return (tmp);
}