File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include "main.h"
2
+
3
+
4
+ /**
5
+ * _realloc - reallocates a memory block
6
+ * @ptr: pointer to the memory previously allocated with a call to malloc
7
+ * @old_size: size of ptr
8
+ * @new_size: size of the new memory to be allocated
9
+ *
10
+ * Return: pointer to the address of the new memory block
11
+ */
12
+
13
+ void * _realloc (void * ptr , unsigned int old_size , unsigned int new_size )
14
+ {
15
+ void * temp_block ;
16
+ unsigned int i ;
17
+
18
+ if (ptr == NULL )
19
+ {
20
+ temp_block = malloc (new_size );
21
+ return (temp_block );
22
+ }
23
+ else if (new_size == old_size )
24
+ return (ptr );
25
+
26
+ else if (new_size == 0 && ptr != NULL )
27
+ {
28
+ free (ptr );
29
+ return (NULL );
30
+ }
31
+ else
32
+ {
33
+ temp_block = malloc (new_size );
34
+ if (temp_block != NULL )
35
+ {
36
+ for (i = 0 ; i < min (old_size , new_size ); i ++ )
37
+ * ((char * )temp_block + i ) = * ((char * ) ptr + i );
38
+ free (ptr );
39
+ return (temp_block );
40
+ }
41
+ else
42
+ return (NULL );
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments