0

I am only experimenting on the basics of Data Structure and I got curious about something. As you can see in my code, after messing around with the p pointer I decided to delete it. I deleted the p pointer, i assumed that it would also delete p->proximo ('proximo' is 'next' in Portuguese) but it didn't apparently. The result of Acesso 3 (Access 3 in Portuguese) was 0 22.

So first of all, if I deleted the pointer, why can I still connect to the p->proximo pointer? Okay, but since it didn't, it is a good thing, it means I can delete things now. So then I decided to make the 22 value go to the first Nod. So I wouldn't have broken nodes on the list. I tried using p = p->proximo as you can see in line 28. But it didn't work. Of course it didn't. I deleted the pointer. is there any way I can recreate the first pointer so I can fix the list, so the first node would have the value of 22? I tried with

p = new IntNode();
p = p->proximo;

but it also failed. Probably because of the existing following node maybe?

I appreciate very much the help of any of you

Code:

#include <iostream>
using namespace std;

class IntNode{
public:
  IntNode(){
    proximo = 0;
  }
  IntNode(int file, IntNode *next = 0)
  {
    number = file;
    proximo = next;
  }
  int number;
  IntNode *proximo;
};

int main()
{
  IntNode *p = new IntNode(15);
  cout << "Acesso 1:  " << p->number;
  p->proximo = new IntNode(22);
  cout << endl << "Acesso 2:  " << p->number << "    " << p->proximo->number;
  delete p;
  cout << endl << "Acesso 3:  " << p->number << "     " << p->proximo->number << endl;
  p = new IntNode(5);
  cout << endl << "Acesso 4:  " << p->number << "     " << p->proximo->number << endl;
  p = p->proximo;
  cout << endl << "Acesso 5:  " << p->number << "     " << p->proximo->number << endl;
  delete p->proximo;
  cout << endl << "Acesso 6:  " << p->number << "     " << p->proximo->number << endl;
  return 0;
}
Veger
  • 37,240
  • 11
  • 105
  • 116
Victor Chavauty
  • 186
  • 3
  • 12

2 Answers2

4

why can i still connect to the p->proximo pointer?

it is undefined behaviour. It just happens that whatever p was pointing for is still there after the delete (see the hotel room analogy from @BoPersson's comment). But you cannot rely on that. If you want to increase the chances of getting a runtime error, or give yourself the ability to check for the validity of p, set it to NULL or (nullptr in C++11):

delete p;
p = nullptr;

next, when you do this:

p = new IntNode(5);

p->proximo is set to null. This is what you have coded yourself, so it shouldn't surprise you:

IntNode(int file, IntNode *next = 0)
{
  number = file;
  proximo = next;
}

Finally, to answer the question in the title,

In c++, after the destruction of a pointer to a class, can i re-utilize the pointer using the new function

Yes, you can.

delete p;
p = someOtherPointerToIntNode;
Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

Delete will deallocate the memory. If you jump to this memory location the data will still be there - unless it gets overwritten. Using it after the delete is considered bad practice as it increases the chance for hard to find bugs.

Udo Klein
  • 6,784
  • 1
  • 36
  • 61
  • 1
    You should never access the memory after freeing it using free() or delete(). The behavior is undefined. And a very high likelihood of segmentation fault. – Tuxdude Feb 27 '13 at 09:23