0
Hi, I have a question. I’m making a program, a social network, I set up a start screen function, in which the application returns to it after a task is selected. For example, register profile.
the application works when I register 1 profile, in the registration of the 2 profile appears this error: If anyone can help, thank you.
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Looks like deleting P1, profile, doesn’t work. How can I create the profile object several times? I don’t know how to do this.
That’s part of the code:
void telaInicio(RedeSocial* r1){
int opcaoinicio=0;
int contt=0;
Perfil **perfis;
cout << r1->quantidadeDePerfis;
cout << endl << "Escolha uma opcao: ";
cout << endl << "1) Cadastar Perfil ";
cout << endl << "2) Cadastrar Disciplina";
cout << endl << "3) Logar";
cout << endl << "0) Terminar " << endl;
cin >> opcaoinicio;
if (opcaoinicio==1){
int numeroper=0;
string nomeper="";
string emailper="";
cin >> numeroper;
cin >> nomeper;
cin >> emailper;
/* Daqui pra baixo está o problema. */
/* Aqui é um construtor normal */
Perfil* p1= new Perfil(numeroper,nomeper,emailper);
/* A função adicionar funciona normalmente */
r1->adicionar(p1);
/* Aqui eu executo a mesma função de novo */
telaInicio(r1);
}
}
Here is the Profile Builder:
Perfil::Perfil(int numero, string nome, string email) :
numero(numero), nome(nome), email(email)
{
}
Add function:
bool RedeSocial::adicionar(Perfil* perfil)
{
if (quantidadeDePerfis<numeroMaximoDePerfis){
this->perfis[quantidadeDePerfis]= perfil;
quantidadeDePerfis+= 1;
return true;
}
return false;
}
When you run the function again tu ta relocate on top of P1 again, I recommend instantiating the profile still inside the add function instead of inside the if
– Junior Nascimento
But if the function is recursive when it will stop ? I see no case of stopping. If it does not stop every time it opens it uses memory for the stack and to allocate a new profile, soon eventually it will crash. Remove the recursiveness that should fail to give this error.
– Isac
problem solved, I had not instantiated the size of the vector profiles of the Redesocial class. Thank you.
– David Ribeiro