-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path100-realloc.c
57 lines (52 loc) · 1.17 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
51
52
53
54
55
56
57
#include "holberton.h"
#include <stdlib.h>
/**
* copyit- a helper function that copies over a string
* @ptr: a pointer. the original og array
* @pointer: the new pointer we want
* @old_size: the size of the old stuff cause we want to copy that
*
* Description: copies the old pointer into the new pointer
* Return: nothing it is void.
*/
void copyit(char *ptr, char *pointer, int old_size)
{
if (old_size)
{
*pointer = *ptr;
copyit(ptr + 1, pointer + 1, old_size - 1);
}
}
/**
* _realloc - write a function that reallocates a memory
* @ptr: a old pointer given to us
* @old_size: the old size of the memory
* @new_size: the new size of the memory
*
* Description: omg there are too many specifics for this
* Return: returns the pointer or nullllllll
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
void *pointer;
if (new_size == old_size)
return (ptr);
if (ptr == NULL)
{
return (malloc(new_size));
}
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
if (new_size > old_size || ptr != NULL)
{
pointer = malloc(new_size);
if (!pointer)
return (NULL);
copyit(ptr, pointer, old_size);
}
free(ptr);
return (pointer);
}