Click here to Skip to main content
15,879,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

delete pbol;

with this code my vector has null pointer, I think that is why I delete pbol, but I must delete it, how can I do?

What I have tried:

I tried to search on internet because I don't solve it
0x01AA 22-Feb-24 7:41am    
It makes no sense deleting pointers after adding them to the vector. You should only delete the pointers when you no longer need m_vBol. Means, you iterate over all items in m_vBol and delete them

Just to add to what 0x01AA has - very rightly - said ...
Copying a pointer does just that - it makes a copy of the address of an item, not a copy of the item.
And when you call delete on a pointer to a memory item the memory it was using is returned to the heap pool for reuse - which means that any copies of the address can now point to a totally different structure.

For example, if you malloc an array of 5 integers, copy the pointer, delete it, and then malloc an array of 100 floats, the copy of the pointer can access the float array and scramble other code's data.

If you need to delete a pointer, it is your responsibility to be sure that it is no longer in use, that there are no copies of the pointer which can be subsequently accessed - otherwise wildly unpredictable errors can start to appear in unrelated code!
Share this answer
The following code
#include <iostream>
#include <vector>
using namespace std;

class Foo
  int m_value;
  Foo(int i):m_value(i){}
  ~Foo(){ cout << "~Foo() called\n";}
  int get_value(){return m_value;}

int main()

  Foo * pfoo = new Foo(42);

  vector <Foo * > v;

  cout << "pfoo " << pfoo << "\n";
  cout << "v[0] " << v[0] << "\n";
  cout << "v[0]->get_value() " << v[0]->get_value() << "\n";

  cout << "deleting pfoo\n";
  delete pfoo;
  cout << "setting pfoo=nullptr\n";
  pfoo = nullptr;

  cout << "pfoo " << pfoo << "\n";
  cout << "v[0] " << v[0] << "\n";
  cout << "v[0]->get_value() " << v[0]->get_value() << "\n";
pfoo 0x55de79f86eb0
v[0] 0x55de79f86eb0
v[0]->get_value() 42
deleting pfoo
~Foo() called
setting pfoo=nullptr
pfoo 0
v[0] 0x55de79f86eb0
v[0]->get_value() 0
on my Linux box (it could be worse).

Have a look at smart pointers -[^].
Share this answer
Maxim Kartavenkov 27-Feb-24 7:40am    
In addition to Palini's solution in which the pointer is encapsulated in a class or alternatively the use of smart pointers, it would also be possible to simply delete the invalid pointer from the vector before deleting it. Then it would no longer cause any problems.
m_vBol.erase(std::find(m_vBol.begin(), m_vBol.end(), pbol));
// or m_vBol.erase(std::remove(std::begin(m_vBol), std::end(m_vBol), pbol), std::end(m_vBol));
delete pbol;

Alternatively, you could overwrite it with nullptr.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900