Dynamic Pointers allocation in C++


  • QA Engineer

    I am needing to create a protective routine in the removal function. Why makes a mistake removing the last item?

    The function:

    // Remover o primeiro cliente
    void cadRemover(){
      lista=ini; // Volta ao início
      aux = lista; // Copia o 1º para aux
      lista = lista->prox; // Move a lista para o próximo
      delete aux; // Deleta aux
    }
    

    The source http://pastebin.com/3iu8h9Qw .



  • The direct answer to your question is that you are using ini as the beginning of the list but when removing an element does not update its value. As it does not do this you will delete the same elements several times causing the error seen on Windows. Without modifying anything else, the code of your function would have to change to:

    // Remover o primeiro cliente
    void cadRemover(){
      lista=ini; // Volta ao início
      aux = lista; // Copia o 1º para aux
      lista = lista->prox; // Move a lista para o próximo
      ini = lista; // Atualiza o novo início da lista
      delete aux; // Deleta aux
    }
    

    However, it is worth noting that this code can be simplified if we take into account that the field prox of the last element of the list is equal to NULL and remove unnecessary variables. We're like this:

    // Remover o primeiro cliente
    void cadRemover(){
      if (ini == NULL)
      {
        return; // Lista vazia.
      }
      cadastro *aux = ini; // Copia o 1º para aux
      ini = ini->prox; // Move a lista para o próximo
      delete aux; // Deleta aux
    }
    

    Now that the problem has been solved I find it important to comment on some things about your code since it has several non-recommended choices.

    • Use of unnecessary global variables

    A clear example of this problem is the variable aux that is declared as global but is only used in the function cadRemover. The same can be said about the variable lista which is only used in cadExibir. You should always try to declare your variables in the nearest possible scope of your use, in these cases within the functions.

    • Multiple expressions in the same line.

    In several sections you put more than one expression on the same line. This practice only makes reading the code difficult and makes certain pieces more confusing.

    Example:

    cout<<"\nEntre com a quantidade de clientes para cadastrar: ";cin>>n;cin.get();
    

    float *alunos, soma=0, media;

    Could be:

    cout<<"\nEntre com a quantidade de clientes para cadastrar: ";
    cin>>n;
    cin.get();

    float *alunos;
    float soma = 0.0f;
    float media;

    • Use using namespace std at the beginning of the program

    This is a very common practice among people who are beginning to use C++ but not advisable. One of the great problems in doing this is name collision (when two structures/functions/etc are declared with the same name). namespaces were introduced into the language to help in the organization of the programs and avoid these collisions especially when using libraries produced by third parties. Therefore, it is recommended to use the fully qualified name of the structures/functions/etc, as std::cout, std::cin and do not use using namespace std.

    • Use int as bool

    You create the variables f1, f2 and f3 to determine whether or not a certain stage was performed. For this it would be more interesting to use type variables bool that was created just to represent true/false.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2