Dynamic Pointer Allocation in C ++

3

I need to create a protection routine in the removal function. Why did you make an error 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 complete .

    
asked by anonymous 09.08.2014 / 14:12

1 answer

3

The direct answer to your question is that you are using ini as the start of the list but removing an element does not update its value. Because it does not do this you delete the same elements several times causing the error seen in Windows. Without changing 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, this code can be simplified if you take into account that the prox field of the last list element equals NULL and removes unnecessary variables. We stay 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 think it is important to comment on some of the things about your code, since it has several choices that you do not recommend.

  • Usage of unnecessary global variables

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

  • Multiple expressions on the same line.

In several sections you put more than one expression on the same line. This practice only makes it difficult to read the code 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 starting to use C ++ but that is not advisable. One of the big problems doing this is name collision (when two structures / functions / etc are declared with the same name). namespace s were introduced in the language to assist in the organization of programs and to avoid these collisions especially when using libraries produced by third parties. Therefore, it is recommended to use the fully qualified name of structures / functions / etc, such as std::cout , std::cin and do not use using namespace std .

  • Using int as bool

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

    
10.08.2014 / 02:04