Dynamic allocation of objects on the free store (part 2)
Revisiting Problematic()
Resizing a dynamically allocated array
Activity: Implementing a function to push values back to a dynamic array with resizing
Graded Playground Autograder
In this problem, you will implement the function PushBackToIntArray
. This function will have four parameters:
int*& array
: an integer array on the free store.unsigned int& size
: the size of the array. This is the number of elements presently in the array.unsigned int& capacity
: the capacity of that array. This is the number of elements it can hold.int value
: the integer value to "push back" to the array.
The complete function signature and return type follows:
void PushBackToIntArray(int*& array, unsigned int& size, unsigned int& capacity, int value);
Carefully observe the argument passing modalities used and consider their implications as you read the remainder of the prompt. Your implementation must invoke
the proper version of delete
at most once; that is, you must have at most one delete statement in your code.
Behavior of PushBackToIntArray
Case 1
Consider the following integer array:
Value (in array ) | 11 | 29 | 34 | |||||||
Index (of array ) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
capacity is 10 ; size is 3 . |
In the example above, there are three integer values in the array. Therefore, the size of the array is 3
. Meanwhile, the capacity of the array is 10
: there are 10-slots in the array which integer values can occupy.
PushBackToIntArray
will add value
to the next available slot in the array
. Since arrays are zero-indexed, notice how size
effectively tells where to "push back" value
. So, for instance, calling PushBackToIntArray
with 14
passed to the parameter value
, the value would be added to the open position of the array, and the array's size
would increase by one:
Value (in array ) | 11 | 29 | 34 | 14 | ||||||
Index (of array ) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
capacity is 10 ; size is 4 . |
Case 2
If PushBackToIntArray
is invoked with size
is equal to capacity
, you must "resize" the array
to twice its original capacity. After that, you would add the value
to the next available position of the array, and the array's size
would increase by one. For instance, consider the following array:
Value (in array ) | 1 | 4 | 9 | |||||||
Index (of array ) | 0 | 1 | 2 | |||||||
capacity is 3 ; size is 3 . |
If we wanted to "push back" the integer value 12
to array
, we would need to create a new dynamically allocated array on the free store, copy the values from the old array into the new array. After that, we would deallocate memory associated with the old array, assign array
the new array's address, and update capacity
.
Value (in array ) | 1 | 4 | 9 | |||||||
Index (of array ) | 0 | 1 | 2 | 3 | 4 | 5 | ||||
capacity is 6 ; size is 3 . |
Finally, we would push the value
to the array
and increment size
by one:
Value (in array ) | 1 | 4 | 9 | 12 | ||||||
Index (of array ) | 0 | 1 | 2 | 3 | 4 | 5 | ||||
capacity is 6 ; size is 4 . |
For Case 2, be sure to also consider the special case when capacity
and size
both equal to zero and when array
is passed to the function with value nullptr
. When capacity
is 0
, multiplying capacity * 2
would result in 0
. Since we want to grow the array, in this special situation, increase capacity to 1
from 0
. The push-back operation then continues as usual.
Graded Files
Only the following files are transferred to the autograder.
solution.hpp
solution.cc