Kaz Kaz - 1 month ago 12
C++ Question

Why do I get bad allocation error in C++?

I have a homework assignment and I do not understand why I am getting a bad allocation. I am unsure if I am using pointers wrong or if I am not deleting them at the proper moment?


Implement a class Person with the following fields:


  • the name

  • a pointer to the person's best friend(a Person*)

  • a popularity counter that indicates how many other people have this person as their best friends



Write a program that reads in a list of names, allocates a new person for each of them, and stores them in a vector
<Person*>
. Then ask the name of the best friend for each of the Person objects. Locate the object matching the friend's name and call a set_best_friend member function to update the pointer and
counter. Finally, print out all Person objects, listing the name, best friend, and popularity counter for each.


Here is my code:

class Person
{
public:
Person(string n);
string get_name();
string get_bf_name();
void set_best_friend(string bf, vector<Person*> list);
int set_counter();
int get_counter();
private:
string name;
string best_friend = "Not Entered.";
int pop_counter = 0;
};

Person::Person(string n)
{
name = n;
}

string Person::get_name()
{
return name;
}

string Person::get_bf_name()
{
return best_friend;
}

int Person::set_counter()
{
return pop_counter++;
}

int Person::get_counter()
{
return pop_counter;
}

void Person::set_best_friend(string bf, vector<Person*> list)
{
best_friend = bf;
for(int counter = 0; counter < list.size(); counter++)
{
if(best_friend == list[counter]->get_bf_name())
list[counter]->set_counter();
}
}

int main()
{
cout << "Please enter a list of names and 0 when done." << endl;
vector<Person*> name_list;
string input;

while(input != "0")
{
cin >> input;
name_list.push_back(new Person(input));
}

for (int i = 0; i < name_list.size(); i++)
{
cout << "What is " << name_list[i]->get_name() << " best friend's name ?" << endl;
cin >> input;
name_list[i]->set_best_friend(input, name_list);

}

for (int j = 0; j < name_list.size(); j++)
{
cout << name_list[j]->get_name() << "'s best friend is: " << name_list[j]->get_bf_name()
<< "and their popularity counter is " << name_list[j]->get_counter() << endl;
}

return 0;


}

Answer

I think the problem is here:

for(int d = name_list.begin(); d < name_list.size(); d++)
{
    delete name_list[d];
}

name_list.begin()is not an int that you can use as index. It's a vector<int>::iterator which encapsulates the vector element(the first one to be precise). To access the encapsulated element you have to use the unary * operator.

Try delete *d; instead of delete name_list[d];.

Oh, and rewrite the for loop like this :

for(vector<int>::iterator d = name_list.begin(); d != name_list.end(); ++d)

++d returns the incremented value of the iterator and the for loop needs to break when the iterator has reached name_list.end(). Otherwise it will run another iteration for name_list.end() and I'm not sure how that will crash your program but it will.