-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path100-realloc.c
More file actions
50 lines (47 loc) · 1.09 KB
/
100-realloc.c
File metadata and controls
50 lines (47 loc) · 1.09 KB
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
#include "simpleshell.h"
/**
* _realloc - realloc func
* @ptr: void ptr
* @old_size: old
* @new_size: new
* Return: void func
*
*/
void *_realloc(void *ptr, size_t old_size, size_t new_size)
{
/* create a new pointer where we will store the value of */
/* the the old pointer (ptr) */
char *new_ptr;
/*count - loop throught the index of each pointer to swap */
/* values between them copy whats in (ptr) into (new_ptr) */
size_t count;
if (ptr == NULL)
{
/*if ptr is NULL set malloc for new_ptr*/
new_ptr = malloc(new_size);
return (new_ptr);
}
if (new_size == 0)
{
free(ptr);
return (NULL);
}
if (new_size == old_size)
return (ptr);
/* malloc for new_ptr */
new_ptr = malloc(new_size);
/* check if new_ptr was successfully allocated */
if (new_ptr == NULL)
{
return (NULL);
}
/* increment count while count is less then old_size and new_size */
/* and copy whats inside of ptr to new_ptr */
for (count = 0; count < old_size && count < new_size; count++)
{
new_ptr[count] = ((char *)ptr)[count];
}
/* don't forget to free ptr */
free(ptr);
return (new_ptr);
}