In this project, I learned about using exit, calloc, and realloc in C.
- tests: Folder of test files. Provided by Holberton School.
- holberton.h: Header file containing prototypes for all functions written in the project.
| File | Prototype |
|---|---|
0-malloc_checked.c |
void *malloc_checked(unsigned int b); |
1-string_nconcat.c |
char *string_nconcat(char *s1, char *s2, unsigned int n); |
2-calloc.c |
char *string_nconcat(char *s1, char *s2, unsigned int n); |
3-array_range.c |
int *array_range(int min, int max); |
100-realloc.c |
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); |
-
0. Trust no one
- 0-malloc_checked.c: C function that returns a
pointer to a newly-allocated space in memory using
malloc.- If
mallocfails, the function causes normal process termination with a status value of98.
- If
- 0-malloc_checked.c: C function that returns a
pointer to a newly-allocated space in memory using
-
1. string_nconcat
- 1-string_nconcat.c: C function that returns a pointer to a
newly-allocated space in memory containing the concatenation of two strings.
- The returned pointer contains
s1followed by the firstnbytes ofs2, null-terminated. - If
nis greater than or equal to the length ofs2, the entire strings2is used. - If
NULLis passed, the function treats the parameter as an empty string. - If the function fails - returns
NULL.
- The returned pointer contains
- 1-string_nconcat.c: C function that returns a pointer to a
newly-allocated space in memory containing the concatenation of two strings.
-
2. _calloc
- 2-calloc.c: C function that returns a pointer to a newly-allocated space
in memory for an array, using
malloc.- Allocates memory for an array of
nmembelements ofsizebytes each. - The memory is set to zero.
- If
nmemb= 0,size=0, or the function fail - returnsNULL.
- Allocates memory for an array of
- 2-calloc.c: C function that returns a pointer to a newly-allocated space
in memory for an array, using
-
3. array_range
- 3-array_range.c: C function that returns a pointer to a
newly-allocated space in memory containing an array of integers.
- The array contains all the values from parameters
mintomax, inclusive, ordered frommintomax. - If
min > maxor the function fails - returnsNULL.
- The array contains all the values from parameters
- 3-array_range.c: C function that returns a pointer to a
newly-allocated space in memory containing an array of integers.
-
4. _realloc
- 100-realloc.c: C function that reallocates a memory block using
mallocandfree.- The parameter
ptris a pointer to the memory previously allocated with a call tomalloc: malloc(old_size). - The paramter
old_sizeis the size, in bytes, of the allocated space forptr. - The paramter
new_sizeis the new size, in bytes, of the new memory block. - The contens of
ptrare copied to the newly-allocated space in the range from the start ofptrup to the minimum ofold_sizeandnew_size. - If
new_size>old_size, the "added" memory is not initialized. - If
new_size==old_size, the function returnsptr. - If
ptrisNULL, the call is equivalent tomalloc(new_size)for all values ofold_sizeandnew_size. - If
new_size= 0 andptris notNULL, the call is equivalent tofree(ptr)and the function returnsNULL.
- The parameter
- 100-realloc.c: C function that reallocates a memory block using
-
5. We must accept finite disappointment, but never lose infinite hope
- 101-mul.c: C program that multiplies two positive numbers.
- Usage:
mul num1 num2. - The function assumes
num1andnum2are passed in base 10. - Prints the result followed by a new line.
- If the number of arguments is incorrect or either of
num1ornum2contains non-digits, the function printsErrorfollowed by a new line and exits with a status of98.
- Usage:
- 101-mul.c: C program that multiplies two positive numbers.