-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSS_realloc.c
More file actions
77 lines (62 loc) · 1.41 KB
/
SS_realloc.c
File metadata and controls
77 lines (62 loc) · 1.41 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
69
70
71
72
73
74
75
76
#include "shell.h"
/**
* _memset - this function fills a block of memory with a constant byte
* @s: parameter showing the pointer to the memory area
* @b: parameter showing the byte to fill *s with
* @n: parameter of the amount of bytes to be filled
* Return: s as a pointer
*/
char *_memset(char *s, char b, unsigned int n)
{
unsigned int a;
for (a = 0; a < n; a++)
s[a] = b;
return (s);
}
/**
* ffree -this function frees dynamically allocated array of strings
* @pp: function parameter indicating string of strings
* Return: void
*/
void ffree(char **pp)
{
char **tmp = pp;
if (!pp)
return;
while (*pp)
{
free(*pp);
pp++;
}
free(tmp);
}
/**
* _realloc - this function reallocates a block of memory to a new size
* @ptr: function parameter indicating a pointer
* @old_size: function parameter indicating the byte size of previous block
* @new_size: function parameter indicating the byte size of new block
* Return: void
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
char *new_ptr = NULL;
char *destination;
unsigned int i;
if (!ptr)
return (malloc(new_size));
if (!new_size)
{
free(ptr);
return (NULL);
}
if (new_size == old_size)
return (ptr);
new_ptr = malloc(new_size);
if (!new_ptr)
return (NULL);
destination = new_ptr;
for (i = 0; i < old_size && i < new_size; i++)
destination[i] = ((char *)ptr)[i];
free(ptr);
return (new_ptr);
}