-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_realloc.c
More file actions
55 lines (48 loc) · 992 Bytes
/
_realloc.c
File metadata and controls
55 lines (48 loc) · 992 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
45
46
47
48
49
50
51
52
53
54
55
#include "main.h"
/**
* _memcpy - copies memory area.
*
* @dest: destination location.
* @src: source location.
* @size: size of buffer to be copied.
*
* Return: pointer to dest.
*/
char *_memcpy(char *dest, char *src, unsigned int size)
{
unsigned int index;
for (index = 0; index < size; index++)
{
dest[index] = src[index];
}
return (dest);
}
/**
* _realloc - reallocates a memory block using malloc and free.
*
* @ptr: old memory location.
* @old_size: old size of the buffer.
* @new_size: new size of the allocated memory.
*
* Return: pointer to the new allocated memory.
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
void *new_ptr;
if (new_size == old_size)
return (ptr);
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
if (ptr == NULL)
return (malloc(new_size));
if (new_size > old_size)
{
new_ptr = malloc(new_size);
_memcpy(new_ptr, ptr, old_size);
free(ptr);
}
return (new_ptr);
}