-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealloc.c
More file actions
60 lines (52 loc) · 1017 Bytes
/
realloc.c
File metadata and controls
60 lines (52 loc) · 1017 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
56
57
58
59
#include "shell.h"
/**
* _memcpy - check code.
* @destination: first pointer.
* @src: second pointer.
* @size: size memory.
* Return: destination.
*/
void *_memcpy(void *destination, const void *src, size_t size)
{
unsigned char *d = (unsigned char *)destination;
const unsigned char *s = (const unsigned char *)src;
size_t i;
for (i = 0; i < size; i++)
{
d[i] = s[i];
}
return (destination);
}
/**
* _getsize - check code.
* @str: pointer.
* Return: size.
*/
int _getsize(const char *str)
{
int size = 0;
while (str[size] != '\0')
{
size++;
}
return (size);
}
/**
* _realloc - reallocate size of a pointer.
* @ptr: pointer.
* @size: memory size.
* Return: new pointer.
*/
void *_realloc(void *ptr, size_t size)
{
void *new_ptr;
size_t old_size, copy_size;
old_size = _getsize(ptr);
copy_size = (old_size < size) ? old_size : size;
new_ptr = malloc(size);
if (new_ptr == NULL)
return (NULL);
new_ptr = _memcpy(new_ptr, ptr, copy_size);
free(ptr);
return (new_ptr);
}