Allocation Practice
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.
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:
src: The source string to be concatenated to the destination stringdest: The destination string to be concatenated into. The contents ofsrcshould be appended todeststarting at its null character(this should behave just as the normalstrcatfunction in string.h would).capacity: The current capacity (in bytes) ofdest.
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 bydestallocated in this case. destis a pointer to a dynamically allocated chunk of memory of sizecapacitybytes. Ifdestis already large enough to hold the concatenated string, you should not attempt re-size it. The contents ofsrcshould instead simply be concatenated intodest, anddestshould be returned.- If
destdoes 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
srcallocated.
Implement the function dynaStrCat as specified above here: