-
Write a function that allocates memory using
malloc
.- Prototype:
void *malloc_checked(unsigned int b);
- Returns a pointer to the allocated memory
- if
malloc
fails, themalloc_checked
function should cause normal process termination with a status value of98
- Prototype:
-
Write a function that concatenates two strings.
- Prototype:
char *string_nconcat(char *s1, char *s2, unsigned int n);
- The returned pointer shall point to a newly allocated space in memory, which contains
s1
, followed by the firstn
bytes ofs2
, and null terminated - If the function fails, it should return
NULL
- If n is greater or equal to the length of
s2
then use the entire strings2
- if
NULL
is passed, treat it as an empty string
- Prototype:
-
Write a function that allocates memory for an array, using
malloc
.- Prototype:
void *_calloc(unsigned int nmemb, unsigned int size);
- The
_calloc
function allocates memory for an array ofnmemb
elements ofsize
bytes each and returns a pointer to the allocated memory. - The memory is set to zero
- If
nmemb
orsize
is0
, then_calloc
returnsNULL
- If
malloc
fails, then_calloc
returnsNULL
- Prototype:
FYI: The standard library provides a different function: calloc
. Run man calloc
to learn more.
-
Write a function that creates an array of integers.
- Prototype:
int *array_range(int min, int max);
- The array created should contain all the values from
min
(included) tomax
(included), ordered frommin
tomax
- Return: the pointer to the newly created array
- If
min
>max
, returnNULL
- If
malloc
fails, returnNULL
- Prototype:
-
Write a function that reallocates a memory block using
malloc
andfree
- Prototype:
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size);
- where
ptr
is a pointer to the memory previously allocated with a call tomalloc
:malloc(old_size)
old_size
is the size, in bytes, of the allocated space forptr
- and
new_size
is the new size, in bytes of the new memory block - The contents will be copied to the newly allocated space, in the range from the start of
ptr
up to the minimum of the old and new sizes - If
new_size
>old_size
, the “added” memory should not be initialized - If
new_size
==old_size
do not do anything and returnptr
- If
ptr
isNULL
, then the call is equivalent tomalloc(new_size)
, for all values ofold_size
andnew_size
- If
new_size
is equal to zero, andptr
is notNULL
, then the call is equivalent tofree(ptr)
. ReturnNULL
Don’t forget to freeptr
when it makes sense
- Prototype:
FYI: The standard library provides a different function: realloc
. Run man realloc
to learn more.
-
Write a program that multiplies two positive numbers.
- Usage:
mul
num1
num2
num1
andnum2
will be passed in base 10- Print the result, followed by a new line
- If the number of arguments is incorrect, print
Error
, followed by a new line, and exit with a status of98
num1
andnum2
should only be composed of digits. If not, print Error, followed by a new line, and exit with a status of98
- You are allowed to use more than 5 functions in your file
- Usage:
-
You can use
bc
(man bc
) to check your results.