Skip to content

Commit f3c92a6

Browse files
committed
task 0
1 parent 4ab3488 commit f3c92a6

3 files changed

Lines changed: 325 additions & 0 deletions

File tree

0x0C-more_malloc_free/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MORE MALLOC
2+
******************

0x0C-more_malloc_free/main.h

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
#ifndef _MAIN_H_
2+
#define _MAIN_H_
3+
4+
5+
/**
6+
* _atoi - converts char to int
7+
*/
8+
9+
int _atoi(char *s);
10+
11+
/**
12+
* _putchar - writes to standard output
13+
*
14+
* @c: character to compare
15+
*
16+
* Return: 1 or 0
17+
*/
18+
19+
int _putchar(char c);
20+
21+
/**
22+
* print_alphabet - func to print alphabet in lowercase
23+
*/
24+
void print_alphabet(void);
25+
26+
/**
27+
* print_alphabet_x10 - func to print lowercase alphabets 10x
28+
*/
29+
30+
void print_alphabet_x10(void);
31+
32+
/**
33+
* _islower - function checking for a lowercase character
34+
*
35+
* Return: 1 if islower or 0
36+
*/
37+
38+
int _islower(int c);
39+
40+
/**
41+
* _isalpha - checks if char is an alphabet
42+
*
43+
* Return: 1 if isalpha, 0 otherwise
44+
*/
45+
46+
int _isalpha(int c);
47+
48+
/**
49+
* print_sign - func to print sign of a number
50+
*
51+
* Return: 1 if positive, -1 if negative, 0 if 0
52+
*/
53+
54+
int print_sign(int n);
55+
56+
/**
57+
* _abs - computes absolute value of an int
58+
*/
59+
60+
int _abs(int);
61+
62+
/**
63+
* print_last_digit - prints last digit of a number
64+
*
65+
*Return: value of last digit
66+
*/
67+
68+
int print_last_digit(int n);
69+
70+
/**
71+
* jack_bauer - prints timer
72+
*
73+
*/
74+
75+
int jack_bauer(void);
76+
77+
/**
78+
* times_table - prints the 9 times table
79+
*/
80+
81+
void times_table(void);
82+
83+
/**
84+
* add - sums up two integers a & b
85+
*
86+
* Return: sum of two ints a,b
87+
*/
88+
89+
int add(int, int);
90+
91+
/**
92+
* print_to_98 - counts from n to 98
93+
*/
94+
95+
void print_to_98(int n);
96+
97+
/**
98+
* print_times_table - prints n times table
99+
*/
100+
101+
void print_times_table(int n);
102+
103+
/**
104+
* reset_to_98 - takes pointer to an int as parameter and resets the value to 98
105+
*/
106+
107+
void reset_to_98(int *n);
108+
109+
/**
110+
* swap_int - swaps values of two integers
111+
*/
112+
113+
void swap_int(int *a, int *b);
114+
115+
116+
/**
117+
* _strlen - returns string length
118+
*/
119+
120+
int _strlen(char *s);
121+
122+
/**
123+
* _puts - prints a string to standard output
124+
*/
125+
126+
void _puts(char *str);
127+
128+
/**
129+
* print_rev - prints a string in reverse
130+
*/
131+
132+
void print_rev(char *s);
133+
134+
/**
135+
* rev_string - reverses a string
136+
*/
137+
138+
void rev_string(char *s);
139+
140+
/**
141+
* puts2 - prints every other character of a string, starting with the first character, followed by a new line.
142+
*/
143+
144+
void puts2(char *str);
145+
146+
/**
147+
* puts_half - prints half of a string
148+
*/
149+
150+
void puts_half(char *str);
151+
152+
/**
153+
* _strcat - concatenates two strings
154+
*/
155+
156+
char *_strcat(char *dest, char *src);
157+
158+
/**
159+
* _strncat - concatenates two strings
160+
*/
161+
162+
char *_strncat(char *dest, char *src, int n);
163+
164+
/**
165+
* _strncpy - copies string
166+
*/
167+
168+
char *_strncpy(char *dest, char *src, int n);
169+
170+
/**
171+
* _strcmp - compares two strings
172+
*/
173+
174+
int _strcmp(char *s1, char *s2);
175+
176+
/**
177+
* _memset - fills the first n bytes of the memory area pointed to by s with the constant byte b
178+
*/
179+
180+
char *_memset(char *s, char b, unsigned int n);
181+
182+
/**
183+
* _memcpy - copies n bytes from memory area src to memory area dest
184+
*/
185+
186+
char *_memcpy(char *dest, char *src, unsigned int n);
187+
188+
/**
189+
* _strchr - locates a character in a string
190+
*/
191+
192+
char *_strchr(char *s, char c);
193+
194+
/**
195+
* _strspn - gets the length of a prefix substring
196+
*/
197+
198+
unsigned int _strspn(char *s, char *accept);
199+
200+
/**
201+
* _strpbrk - searches a string for any of a set of bytes.
202+
*/
203+
204+
char *_strpbrk(char *s, char *accept);
205+
206+
/**
207+
* _strstr - locates a substring
208+
*/
209+
210+
char *_strstr(char *haystack, char *needle);
211+
212+
/**
213+
* prints_chessboard - prints chessboard
214+
*/
215+
216+
void print_chessboard(char (*a)[8]);
217+
218+
/**
219+
* print_diagsum - prints the sum of the two diagonals of a square matrix of integers.
220+
*/
221+
222+
void print_diagsums(int *a, int size);
223+
224+
/**
225+
* _puts_recursion - priints a string
226+
*/
227+
228+
void _puts_recursion(char *s);
229+
230+
/**
231+
* _print_rev_recursion - prints a string in reverse
232+
*/
233+
234+
void _print_rev_recursion(char *s);
235+
236+
/**
237+
* _strlen_recursion - returns the length of a string
238+
*/
239+
240+
int _strlen_recursion(char *s);
241+
242+
/**
243+
* factorial - returns the factorial of a given number
244+
*/
245+
246+
int factorial(int n);
247+
248+
/**
249+
* _pow_recursion - returns the value of x raised to power y
250+
*/
251+
252+
int _pow_recursion(int,int);
253+
254+
/**
255+
* _sqrt_recursion - returns natural square root of number
256+
*/
257+
258+
int _sqrt_recursion(int n);
259+
260+
/**
261+
* is_prime_number - returns 1 if prime
262+
*/
263+
264+
int is_prime_number(int n);
265+
266+
/**
267+
* create_array - creates an array of chars
268+
*/
269+
270+
char *create_array(unsigned int size, char c);
271+
272+
/**
273+
* _strdup - returns a pointer to a newly allocated space in memory
274+
*/
275+
276+
char *_strdup(char *str);
277+
278+
/**
279+
* str-concat - concatenates two strings
280+
*/
281+
282+
char *str_concat(char *s1, char *s2);
283+
284+
/**
285+
* alloc_grid - returns pointer to a 2d array of ints
286+
*/
287+
int **alloc_grid(int width, int height);
288+
289+
/**
290+
* free_grid - frees grid creatd by alloc_grid func
291+
*/
292+
293+
void free_grid(int **grid, int height);
294+
295+
/**
296+
* malloc_checked - memory allocator
297+
*/
298+
299+
void *malloc_checked(unsigned int b);
300+
301+
#endif
302+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "main.h"
2+
#include <stdlib.h>
3+
4+
/**
5+
* malloc_checked - allocates memory using malloc
6+
* @b: integer to check
7+
* Return: a pointer to void
8+
*/
9+
10+
void *malloc_checked(unsigned int b)
11+
{
12+
void *ptr = malloc(b);
13+
14+
if (ptr == NULL)
15+
{
16+
exit(98);
17+
}
18+
19+
return ptr;
20+
}
21+

0 commit comments

Comments
 (0)