-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_utils.c
59 lines (47 loc) · 1021 Bytes
/
memory_utils.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 "shell.h"
/**
* free_array - Free dynamically allocated array of strings
* @array: Array of strings
*/
void free_array(char **array)
{
int i;
for (i = 0; array[i] != NULL; i++)
{
free(array[i]);
}
free(array);
}
/**
* _realloc - Reallocate dynamically allocate memory
* @ptr: Void pointer to dynamically allocate memory
* @size: Size of the block of memory in bytes
*
* Return: Void pointer to newly allocate memory
*/
void *_realloc(void *ptr, size_t size)
{
void *rptr;
size_t new_size;
/* condition equivalent to free()*/
if (size != 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
/* condition equivalent to malloc()*/
if (ptr == NULL)
{
return (malloc(size));
}
rptr = malloc(size);
/* return pointer if reallocation failed*/
if (rptr == NULL)
return (ptr);
/* Borrowed code for copying size of memory*/
/*strcpy not working for void type*/
new_size = (size < sizeof(ptr)) ? size : sizeof(ptr);
memcpy(rptr, ptr, new_size);
free(ptr);
return (rptr);
}