-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory.c
More file actions
68 lines (66 loc) · 1.49 KB
/
memory.c
File metadata and controls
68 lines (66 loc) · 1.49 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "holberton.h"
/**
*_realloc - reallocates a memory block
*@ptr: is a pointer to the memory previously allocated
*@old_size: is the size, in bytes, of the allocated space for ptr
*@new_size: is the new size, in bytes of the new memory block
*
* Return: pointer to the new allocation with the values from prev
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
char *p, *ptr1 = ptr;
unsigned int iterator = 0;
if (new_size == old_size)
return (ptr);
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
p = malloc(new_size);
if (p == NULL)
{
free(ptr);
return (NULL);
}
while (iterator < new_size - 1)
{
p[iterator] = ptr1[iterator];
iterator++;
}
free(ptr);
return (p);
}
/**
*realloc_pointer - reallocates a memory block
*@ptr: is a pointer to the memory previously allocated
*@old_size: is the size, in bytes, of the allocated space for ptr
*@new_size: is the new size, in bytes of the new memory block
*Return: pointer to the new allocation with the values from prev
*/
void *realloc_pointer(void *ptr, unsigned int old_size, unsigned int new_size)
{
char **p, **ptr1 = ptr;
unsigned int iterator = 0;
if (new_size == old_size)
return (ptr);
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
p = malloc(new_size * sizeof(char *));
if (p == NULL)
{
free(ptr);
return (NULL);
}
while (iterator < new_size - 1)
{
p[iterator] = ptr1[iterator];
iterator++;
}
free(ptr);
return (p);
}