C++ Dynamic Pointer Allocation

Asked

Viewed 218 times

3

I am needing to create a protection routine in the removal function. Why it gives error when 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.

  • I found a way, but I believe it is not yet the best option... (I am commenting because I still do not have enough score - 10 - to post reply)

  • Could be a restriction of timing, but not by reputation: http://answall.com/help/privileges. . . . I edited your question to make the text more fluid, but I was in doubt about the title, please check if it corresponds to your problem.

  • What’s the mistake? Be more specific.

  • I thought it became more intelligible yes @brasofilo, thanks !!

  • @dxhj, the function as it is in the question generates the following error when removing the last item: double free or corruption (fasttop): 0x00000000010cf050 ***
Aborted (core dumped). I was able to solve with an if Else, but it’s still not ideal because it doesn’t use pointer arithmetic...

  • This is because you are trying to release something that is already released.

  • I imagine the reason is that the latter receives an index 0. And, since the array cannot contain negative indices, the error occurs. As I did not find another way, I did the check by if...

Show 2 more comments

1 answer

3

The direct answer to your question is that you are using ini as the beginning of the list but removing an element does not update its value. As it does not do so you 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 in the list is equal to NULL and remove unnecessary variables. This leaves us with:

// 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 a few things about your code as it presents several choices not recommended.

  • Use of unnecessary global variables

A clear example of this problem is the variable aux which 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 scope as close as possible to their use, in such cases within the functions.

  • Multiple expressions on the same line.

In several parts you put more than one expression in 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 it is not advisable. One of the big problems in doing this is name collision (when two structures/functions/etc are declared with the same name). namespaces were introduced in 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/etc structures/functions, such as std::cout, std::cin and not use using namespace std.

  • Use of int as bool

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

Browser other questions tagged

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