Skip to content

Latest commit

 

History

History

0x0C-more_malloc_free

0x0C. C - More malloc, free

Learning objectives for this project:

  • How to use the exit function
  • What are the functions calloc and realloc from the standard library and how to use them

Header File 📁

  • HEADER FILE: Contains function prototypes for all programs in this directory.

Tests ✔️

  • tests: Folder of test files. Provided by ALX SE

Tasks 📃

  • 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 of 98.
  • 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 first n bytes of s2, null-terminated.
      • If n is greater than or equal to the length of s2, the entire string s2 is used.
      • If NULL is passed, the function treats the parameter as an empty string.
      • If the function fails - returns NULL.
  • 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 of size bytes each.
      • The memory is set to zero.
      • If nmemb = 0, size = 0, or the function fail - returns NULL.
  • 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 to max, inclusive, ordered from min to max.
      • If min > max or the function fails - returns NULL.
  • 4. _realloc

    • 100-realloc.c: C function that reallocates a memory block using malloc and free.
      • The parameter ptr is a pointer to the memory previously allocated with a call to malloc: malloc(old_size).
      • The paramter old_size is the size, in bytes, of the allocated space for ptr.
      • 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 of ptr up to the minimum of old_size and new_size.
      • If new_size > old_size, the "added" memory is not initialized.
      • If new_size == old_size, the function returns ptr.
      • If ptr is NULL, the call is equivalent to malloc(new_size) for all values of old_size and new_size.
      • If new_size = 0 and ptr is not NULL, the call is equivalent to free(ptr) and the function returns NULL.
  • 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 and num2 are passed in base 10.
      • Prints the result followed by a new line.
      • If the number of arguments is incorrect or either of num1 or num2 contains non-digits, the function prints Error followed by a new line and exits with a status of 98.