- How to use the
exit
function - What are the functions
calloc
andrealloc
from the standard library and how to use them
- HEADER FILE: Contains function prototypes for all programs in this directory.
- tests: Folder of test files. Provided by ALX SE
-
0. Trust no one
- 0-malloc_checked.c: C function that returns a
pointer to a newly-allocated space in memory using
malloc
.- If
malloc
fails, 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
s1
followed by the firstn
bytes ofs2
, null-terminated. - If
n
is greater than or equal to the length ofs2
, the entire strings2
is used. - If
NULL
is 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
nmemb
elements ofsize
bytes 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
min
tomax
, inclusive, ordered frommin
tomax
. - If
min > max
or 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
malloc
andfree
.- The parameter
ptr
is a pointer to the memory previously allocated with a call tomalloc: malloc(old_size)
. - The paramter
old_size
is the size, in bytes, of the allocated space forptr
. - The paramter
new_size
is the new size, in bytes, of the new memory block. - The contens of
ptr
are copied to the newly-allocated space in the range from the start ofptr
up to the minimum ofold_size
andnew_size
. - If
new_size
>old_size
, the "added" memory is not initialized. - If
new_size
==old_size
, the function returnsptr
. - If
ptr
isNULL
, the call is equivalent tomalloc(new_size)
for all values ofold_size
andnew_size
. - If
new_size
= 0 andptr
is 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
num1
andnum2
are passed in base 10. - Prints the result followed by a new line.
- If the number of arguments is incorrect or either of
num1
ornum2
contains non-digits, the function printsError
followed by a new line and exits with a status of98
.
- Usage:
- 101-mul.c: C program that multiplies two positive numbers.