-1

My code is,

//Pointer to a Vector
std::vector<int> *ptr =  new std::vector<int>;
ptr->push_back(20);
ptr->push_back(30);
ptr->push_back(40);
std::vector<int>::const_iterator pend = ptr->end();
for(std::vector<int>::const_iterator it = ptr->begin(); it != pend;  ++it){
    cout<<*it<<endl;
}
ptr->clear();
delete ptr;
pend = ptr->end();
for(std::vector<int>::const_iterator it = ptr->begin(); it != pend;  ++it){
    cout<<*it<<endl;
}


//Normal Vector
std::vector<int> nptr= {20,30,40};
std::vector<int>::const_iterator end = nptr.end();
for(std::vector<int>::const_iterator it = nptr.begin(); it != end;  ++it){
    cout<<*it<<endl;
}
nptr.clear();
end = nptr.end();
for(std::vector<int>::const_iterator it = nptr.begin(); it != end;  ++it){
    cout<<*it<<endl;
}

In the above sample code, I am iterating the pointer to vector and vector containers before and after clearing the container. In the normal vector case, the begin and end pointers are denoting the same element after done clear, it means the container is empty. In the pointer to a vector, the begin and end pointer never reset after using clear and delete the memory associated with the vector.

The output of the above code,

//pointer to vector before clear
20
30
40
//pointer to vector after clear
29006928
0
33
0
//vector before clear
20
30
40
//vector after clear
**no output**
Robert
  • 67
  • 5
  • 3
    What's your question? You `delete` the `ptr` and then you call `ptr->end()`, clearly that's undefined behavior. – Blaze Mar 06 '19 at 07:12
  • My question is simple, why the loop is iterate after delete. Is delete is get back the memory associated with the vector?. – Robert Mar 06 '19 at 07:25
  • So your question is "Why does undefined behavior manifest in *this* way and not in *that* way"? The answer is "there is no telling what it's going to do". It could give no output, it could give any weird output imaginable, it could crash the program, or do anyhing else. There's no rule telling the compiler what it has to do in this case, so there's no point pondering why it's doing what it does. That's why people don't like undefined behavior; it's unreliable and non-portable. – Blaze Mar 06 '19 at 07:29
  • There is a major difference between the two; the first clears *and destroys* the vector and then keeps using it (which is undefined since it doesn't exist), while the second only clears it. – molbdnilo Mar 06 '19 at 07:35
  • please add the question from your comment to the question. Currently it is unclear what this quesiton is about by reading the question only – 463035818_is_not_an_ai Mar 06 '19 at 08:48
  • Please mark the answer correct if it is :) – Destructor Mar 06 '19 at 18:51

2 Answers2

2

when you call the delete ptr; you are de allocating the memory you reserved for the pointer object. And then you do pend = ptr->end(); This will surely invoke undefined behavior because you don't have any std::vector<int> where you are pointing.

It is best practice to set the value of the pointer to NULL when you delete it. Because when you delete, you delete the object you point at not the content of the pointer. So the pointer is STILL pointing to the same memory location. When you set it to NULL in case if someone is trying to use that again, they would know that the pointer points to nothing.

ptr = nullptr;

Always keep in mind to do a NULL check before de-referencing a pointer

Destructor
  • 523
  • 1
  • 3
  • 13
0

When you call delete on ptr you are destroying the vector and all of its elements.

Dereferencing (i.e. *ptr or ptr->) a deleted pointer is undefined behaviour.

When an object is deleted the memory it pointed to doesn't necessarily disappear (it might no longer be accessible if a whole memory page is no longer used) so dereferencing the pointer might not cause a crash. In your particular environment it looks like the memory isn't immediately overwritten (some environments will immediately overwrite deallocated memory, e.g. visual studio debug builds) enough of the vector's internal state is preserved that your loop manages to still print 4 values (probably the capacity of the vector before deletion). You can't rely on this behaviour though, it could print 0 values, 1000 values, crash or any other undefined behaviour.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60