-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathre-alloc.c
More file actions
41 lines (35 loc) · 677 Bytes
/
re-alloc.c
File metadata and controls
41 lines (35 loc) · 677 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
#include "shell.h"
/**
* re_alloc - allocate memory
* @ptr: pointer
* @o_mem: previously allocated
* @n_mem: reallocate
* Return: reallocated memory
*/
void *re_alloc(void *ptr, unsigned int o_mem, unsigned int n_mem)
{
void *pointer;
unsigned int m;
if (n_mem == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
if (n_mem == o_mem)
return (ptr);
if (ptr == NULL)
{
pointer = malloc(n_mem);
if (pointer == NULL)
return (NULL);
else
return (pointer);
}
pointer = malloc(n_mem);
if (pointer == NULL)
return (NULL);
for (m = 0; m < o_mem && m < n_mem; m++)
*((char *)pointer + m) = *((char *)ptr + m);
free(ptr);
return (pointer);
}