Here's part of a function:
std::queue<std::string>* shunting_yard(std::vector<std::string> input){
std::queue<std::string>* output = new std::queue<std::string>();
...
return output;
I've dynamically allocated queue output
is pointing to on the heap. However, when I try to free/delete the queue in int main()
I get an error because output
is declared out of scope.
Considering the fact that I'm returning output
in the above function- so I can't free it inside the function- how can I make sure that there is not a memory leak? One potential solution I've thought of would be declaring std::queue<std::string>* output
in the global scope and output = new std::queue<std::string>()
inside the function, but are there any other solutions to this out of scope error?