2

This is the example code:

Node *n1 = new Node();
Node *n2 = new Node();
Node *n3 = new Node();

qWarning() << "Setting node names";
n1->setName("N1");
n2->setName("N3");
n3->setName("N3");

qWarning() << "Creating list";
QList<Node*> list;

list << n1 << n2 << n3;

qWarning() << "List iteration:";
for (int i = 0; i < list.size(); i++){
    qWarning() << "Node:" << list.at(i)->getName();
}

if (n1 != NULL){
    qWarning() << "N1 EXISTS";
}
else{
    qWarning() << "N1 DOES NOT EXIST";
}

qWarning() << "Clearing list, CLEARING EVERYTHING";
//list.clear();

while (list.size() > 0){
    Node *n = list.first();
    list.removeFirst();
    delete n;
}

qWarning() << "Accessing nodes";

if (n1 != NULL){
    qWarning() << "N1 EXISTS";
}
else{
    qWarning() << "N1 DOES NOT EXIST";
}

The problem is that both times the printed output is N1 EXISTS. However, clearly, If I try to access n1 after the accessing nodes message, the program will crash. How can I check that the data structure to which n1 points, actually exists?

Rama
  • 3,222
  • 2
  • 11
  • 26
aarelovich
  • 5,140
  • 11
  • 55
  • 106
  • 4
    You can't - you have so called *dangling pointers*. Accessing the element they point to invokes *undefined behavior* – UnholySheep Mar 20 '17 at 12:58
  • 4
    This is why you use smart pointers. They can actually express ownership and the lifetime can be queried. – NathanOliver Mar 20 '17 at 12:58
  • Ok. So the only way to prevent this is to make sure that this situation, doesn't happen. Correct? – aarelovich Mar 20 '17 at 12:59
  • You could explicitly set it to `nullptr` (or `NULL`) after deleting it. – 001 Mar 20 '17 at 12:59
  • It's your job to keep track of all references or pointer to an object and use / dereference them only if the object is alive. – Pixelchemist Mar 20 '17 at 12:59
  • 1
    The more you learn about programming, the more you'll be thankful that this _doesn't_ have the behavior you were expecting. – paddy Mar 20 '17 at 12:59
  • 2
    You should restructure your program so it is clear who owns what. Value semantics get you 99% there, and 99% of the rest of the time a `std::unique_ptr` will do and if that is still not enough `std::weak_ptr` + `std::shared_ptr` have exactly the feature you want, but you probably don't actually need it. – nwp Mar 20 '17 at 12:59
  • Also see: http://stackoverflow.com/questions/1930459/c-delete-it-deletes-my-objects-but-i-can-still-access-the-data – NathanOliver Mar 20 '17 at 12:59
  • I guess this question gives you an answer: http://stackoverflow.com/questions/704466/why-doesnt-delete-set-the-pointer-to-null – Ervin Szilagyi Mar 20 '17 at 12:59
  • I don't know what are smart pointers. Could you give me some info on it? Or a link with examples? – aarelovich Mar 20 '17 at 13:00
  • 1
    @aarelovich http://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one – NathanOliver Mar 20 '17 at 13:01
  • Reference: http://en.cppreference.com/w/cpp/memory – UnholySheep Mar 20 '17 at 13:01

1 Answers1

4

Deleting a pointer doesn't deletes the pointer itself, it only frees up the memory that pointer was pointing to. Once you delete a pointer and then if you try to access the memory location that the pointer pointed to, it will invoke undefined behavior.

Yousaf
  • 27,861
  • 6
  • 44
  • 69