I have a vector of pointers like so: vector<Operators*> valid_start = {&negative, &values, &numbers, &bracket_left, &functions};
When using the following code to deallocate the memory, the program crashes
void deallocate_vector(vector<Operators*> & a)
{
for(int it = 0; it < a.size(); it++)
{
delete a[it];
}
a.clear();
}
I've looked at these threads
Does vector::erase() on a vector of object pointers destroy the object itself?
Cleaning up an STL list/vector of pointers
Deallocating objects stored in a vector?
and cannot see why this code fails. I've tried this form of deallocation as well, and this also crashes:
for (std::vector<Operators*>::iterator it = a.begin(); it != a.end(); ++it)
{
delete(*it);
}
a.clear();
I have effectively copied code verbatim, and it still fails. Why?
EDIT:
The declaration of negative. The rest of the pointers are of the same type
struct Operators
{
std::vector<std::string> members;
std::vector<Operators*> precedents;
std::vector<Operators*> followers;
}negative;
negative.members = {"-"};
negative.precedents = {&basic_operators, &values, &numbers, &comma, &bracket_left, &bracket_right, &factorial};
negative.followers = {&bracket_left, &numbers, &values, &functions};