Dynamic Arrays
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.
In this exercise you will learn how containers in high-level languages such as Python lists or C++ std::vector are implemented internally. We will implement a dynamic array (vector) in C that automatically resizes as elements are inserted and deleted.
Static Arrays
Let's take a look at how static arrays could be implemented. We can think about a simple fixed-size int array struct like this:
#define CAPACITY 100
struct static_array {
int num_elements;
int data[CAPACITY];
};
When declared as a local variable, the struct will be stored on the stack, which has limited memory. This approach is fine when CAPACITY is small, the type being stored is small, and the maximum number of elements is known at compile time. If any of those are not true, then it's time to think about another way of storing data.
This is where dynamic arrays (also known as Vectors in C++) come into play. They keep their data on the heap and can resize their capacity at runtime.
Heap-Based int Vector Representation
Lets take a look at our dynamic vector struct below that can dynamically resize to hold an arbitrary number of integers. size represents the number of elements in the array. This is not to be confused with capacity which represents how many elements the array data can hold. Then data is the array of all of the elements in the vector. Note, the size_t type. This is an unsigned integer type that is guaranteed to have enough bits to represent the maximum possible index into an array.
typedef struct vector_t {
size_t size;
size_t capacity;
int* data;
} vector_t;
Meaning of Fields
| Field | Meaning |
|---|---|
size |
number of elements currently stored |
capacity |
number of elements that can fit before resizing |
data |
pointer to heap memory storing elements |
Example:
size = 3
capacity = 8
The vector contains 3 elements but has space for 5 additional elements before resizing.
Vector Interface
The int vector we're going to implement will support the following methods:
// Take a pointer to an int vector and initialize it to be empty with capacity 1.
void vector_init(vector_t* vec);
// Free a vector's allocated memory
void vector_destroy(vector_t* vec);
// Append an element to the end of the vector. Doubles capacity if needed.
void vector_push_back(vector_t* vec, int element);
// Remove the last element
void vector_pop_back(vector_t* vec);
// Return the element at index "index".
int vector_get(vector_t* vec, size_t index);
// Insert element at index "index" in vector. Doubles capacity if needed.
void vector_insert(vector_t* vec, int element, size_t index);
// Remove element at index "index" in vector. Shifts elements left to fill space.
void vector_erase(vector_t* vec, size_t index);
Example Usage
int main(){
struct vector_t my_vector;
vector_init(&my_vector); // Initialize vector
// %zu format for size_t
printf("%zu\n", my_vector.capacity); // expected: 1 Initial allocation
printf("%zu\n", my_vector.size); // expected: 0 (vector is empty)
vector_push_back(&my_vector, 5);
vector_push_back(&my_vector, 10);
vector_push_back(&my_vector, 15);
vector_push_back(&my_vector, 20);
vector_push_back(&my_vector, 25);
printf("%zu\n", my_vector.size); // expected: 5
printf("%zu\n", my_vector.capacity); // expected: 8 (capacity)
printf("%d\n", vector_get(&my_vector, 0)); // expected: 5
printf("%d\n", vector_get(&my_vector, 1)); // expected: 10
printf("%d\n", vector_get(&my_vector, 2)); // expected: 15
printf("%d\n", vector_get(&my_vector, 3)); // expected: 20
printf("%d\n", vector_get(&my_vector, 4)); // expected: 25
// printf("%d\n", vector_get(&my_vector, 10)); // Unknown, undefined behavior
vector_pop_back(&my_vector);
// Vector was originally [5, 10, 15, 20, 25] then becomes [5, 10, 15, 20]
printf("%zu\n", my_vector.size); // expected: 4
vector_erase(&my_vector, 0);
// Vector was originally [5, 10, 15, 20] then becomes [10, 15, 20]
printf("%zu\n", my_vector.size); // expected: 3
printf("%d\n", vector_get(&my_vector, 0)); // expected: 10
vector_insert(&my_vector, 25, 1);
// Vector was originally [10, 15, 20] then becomes [10, 25, 20, 15]
printf("%zu\n", my_vector.size); // expected: 4
printf("%d\n", vector_get(&my_vector, 1)); // expected: 25
vector_destroy(&my_vector); // Deallocate memory
}
You may assume malloc and realloc return successfully in all functions below.
Initialize
vector_init takes a pointer to a vector vec and initializes it to be empty with capacity 1.
Destroy
vector_destroy takes a pointer to a vector vec and frees the memory of the data array. Other fields do not need to be changed.
Push Back
vector_push_back takes a pointer to a vector vec and an int element and appends the element to the end of the vector. If adding the element will go over the capacity of the array, double the capacity of the array either by doing a malloc and moving everything, or using realloc.
Pop Back
vector_pop_back takes a pointer to an vector vec and removes the last element of the vector. Pop back on an empty vector will be considered undefined behavior.
Get
vector_get takes a pointer to a vector vec and index then returns the element at index index. An out of bounds access will be considered undefined behavior.
Insert
vector_insert takes a pointer to a vector vec, element, and index, then inserts element at index index and shifts elements to the right to make room. If index == vec->size then the vector_insert will act like vector_push_back. Remember to allocate space if the vector runs out of capacity. A value of index greater than vec->size will be considered undefined behavior.
Erase
Finally, vector_erase takes a pointer to a vector vec and index, then remove the element at index index and shifts elements to the left to fill the empty space. A value of index out of bounds will be considered undefined behavior.