Solved - Error running the program, please help. terminate called after'Std::bad_alloc'

Asked

Viewed 205 times

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

  • 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.

  • problem solved, I had not instantiated the size of the vector profiles of the Redesocial class. Thank you.

1 answer

0

std::bad_alloc is an exception that occurs when it is not possible to allocate memory to create an object.

One thing that’s got me intrigued is your job telaInicio be recursive and see no reason for it. As we do not see the whole code (who knows what has in the part (...)) the problem may be there. In particular, what are you doing with the variable Perfil **perfis inside telaInicio.

It may not be the direct cause of the problem, but it is worth rewriting so that it is not recursive, because as you use the program the function call stack will continue to grow until the program is terminated resulting in something like

telaInicial -> (chama) telaInicial -> (chama) telaInicial -> ...

Another point is that it is not considered good practice to use new nowadays. Use a smart Pointer instead of a raw pointer to manage memory.

Ex: If you want to create an object Perfil walk in telaInicio then do

auto p1 = std::make_unique<Perfil>(numeroper,nomeper,emailper);
r1->adicionar(std::move(p1));

The function RedeSocial::adicionar must have the signature.

bool RedeSocial::adicionar(std::unique_ptr<Perfil> perfil)

and if you try to call adicionar without doing the std::move will give build error (because std::unique_ptr cannot be copied).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.