-
Notifications
You must be signed in to change notification settings - Fork 0
/
memwares.c
46 lines (39 loc) · 862 Bytes
/
memwares.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
#include "main.h"
/**
* _realloc - reallocate memory block
* @ptr: pointer to address of memory block to reallocate
* @old_size: previous size of the memory
* @new_size: new size of the memory
*
* Description: a function that reallocates a memory block using malloc & free
* Return: pointer to the new allocated memory
* if ptr = NULL, return NULL
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
if (ptr == NULL)
ptr = malloc(new_size);
if (new_size == old_size)
return (ptr);
free(ptr);
ptr = malloc(new_size);
return (ptr);
}
/**
* freearray - freee an array of string pointers
* @array: array of string pointers
*
* Return: void
*/
void freearray(char **array)
{
int i = 0;
while (array[i])
free(array[i++]);
free(array);
}