-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemfunc.c
More file actions
executable file
·99 lines (81 loc) · 1.88 KB
/
memfunc.c
File metadata and controls
executable file
·99 lines (81 loc) · 1.88 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "main.h"
/**
* _memcpy - function that copies memory area
* @dest: memory area to copy to
* @src: memory area to copy from
* @n: number of bytes to be filled
*
* Return: returns pointer to memory area dest
*/
char *_memcpy(void *dest, const void *src, unsigned int n)
{
unsigned int i;
char *dest_cpy = (char *)dest;
char *src_cpy = (char *)src;
for (i = 0; i < n; i++)
*(dest_cpy + i) = *(src_cpy + i);
return (dest_cpy);
}
/**
* _realloc - reallocates a memory block of a pointer
* @ptr: double pointer to the memory previously allocated
* @old_size: size, in bytes, of the allocated space of ptr
* @new_size: new size, in bytes, of the new memory block
*
* Return: ptr
* if new_size == old_size, returns ptr without changes.
* if malloc fails, returns NULL
*/
char *_realloc(char *ptr, unsigned int old_size, unsigned int new_size)
{
char *newptr;
unsigned int i;
if (ptr == NULL)
return (malloc(sizeof(char *) * new_size));
if (new_size == old_size)
return (ptr);
newptr = malloc(sizeof(char *) * new_size);
if (newptr == NULL)
return (NULL);
for (i = 0; i < old_size; i++)
newptr[i] = ptr[i];
free(ptr);
return (newptr);
}
/**
* _reallocdoublep - reallocates memory for a double pointer
* @ptr: old double pointer
* @size: size of old pointer
* @new_size: new size of new pointer
*
* Return: new pointer
*/
char **_reallocdoublep(char **ptr, unsigned int size, unsigned int new_size)
{
char **newptr;
unsigned int i;
if (new_size == 0)
{
free(ptr);
return (NULL);
}
if (ptr == NULL)
return (malloc(sizeof(char *) * new_size));
if (new_size == size)
return (ptr);
newptr = malloc(sizeof(char *) * new_size);
if (newptr == NULL)
return (NULL);
if (size <= new_size)
{
for (i = 0; i < size; i++)
newptr[i] = ptr[i];
}
else
{
for (i = 0; i < new_size; i++)
newptr[i] = ptr[i];
}
free(ptr);
return (newptr);
}