diff --git a/ps4/src/symbols.c b/ps4/src/symbols.c index 2758762..ae9c5d3 100644 --- a/ps4/src/symbols.c +++ b/ps4/src/symbols.c @@ -225,17 +225,12 @@ static size_t string_list_capacity; // Takes ownership of the string, and returns its position in the string list. static size_t add_string(char* string) { - // TODO: Write a helper function you can use during bind_names(), - // to easily add a string into the dynamically growing string_list. - - // The length of the string list should be stored in string_list_len. - - // The variable string_list_capacity should contain the maximum number of char* - // that can fit in the current string_list before we need to allocate a larger array. - // If length is about to surpass capacity, create a larger allocation first. - // Tip: See the realloc function from the standard library - - // Return the position the added string gets in the list. + if (string_list_len >= string_list_capacity) { + string_list_capacity = string_list_capacity * 2 + 8; + string_list = realloc(string_list, string_list_capacity * sizeof(char*)); + } + string_list[string_list_len] = string; // takes ownership + return string_list_len++; } // Prints all strings added to the global string list