Allocation Practice

The questions below are due on Sunday November 15, 2026; 11:59:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Back to Exercises

The practice problems on this page are here to offer you a little bit of additional practice with the malloc, realloc, and free functions before writing more complex functions with them.

Array Sizing

First X elements

You'll be writing a function firstX below that takes in a pointer, inp_ptr, to an allocated array of elements of secret_type_t on the heap. The function should return a pointer to the newly allocated chunk of memory containing the first x elements of said array. Your implementation should allocate the minimum necessary number of bytes needed to store the x elements.

Make sure your implementation of the function has no memory leaks. You may assume that x will be greater than 0 and less than the total number of elements in the array.

Any X elements

Assume that you now have a dynamically allocated array pointed to by inp_ptr. The array's elements are of type secret_type_t. We would like to re-size the array such that the first x elements starting at index i (where i is 0-indexed) are stored contiguously in a single chunk of memory on the heap. Any unused memory after the re-sizing should be freed and returned to the available memory pool.

Write the function, anyX, below that implements this behavior. You should once again be careful not to introduce any memory leaks in writing your implementation. As with the previous problem, you should allocate the minimum required number of bytes needed to store said x elements.

For this function, if x equals 0, you should simply de-allocate the entire chunk of memory pointed to by inp_ptr and return NULL.

String Sizing

You'll be implementing a custom string concatenation function dynaStrCat that takes in three inputs:

  1. src: The source string to be concatenated to the destination string
  2. dest: The destination string to be concatenated into. The contents of src should be appended to dest starting at its null character(this should behave just as the normal strcat function in string.h would).
  3. capacity: The current capacity (in bytes) of dest.

A few notes:

  • If there no longer is enough space on the heap to store the concatenated string, your function should simply return NULL. Your function should also keep the chunk of memory pointed to by dest allocated in this case.
  • dest is a pointer to a dynamically allocated chunk of memory of size capacity bytes. If dest is already large enough to hold the concatenated string, you should not attempt re-size it. The contents of src should instead simply be concatenated into dest, and dest should be returned.
  • If dest does not have a large enough capacity to hold the concatenated string and you choose to re-size, any newly-allocated memory chunk should have the minimum required size needed to store the concatenated string properly.
  • Your function should always keep the chunks of memory pointed to by src allocated.

Implement the function dynaStrCat as specified above here:

Back to Exercises