Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Code as:
C++
int main()
{
   int* p = new int;
   int* p2 = p;

   delete p2;
   p2 = NULL;

   return 0;
}

so the question is if variable p is a wild pointer?

What I have tried:

C++
typedef struct List_Tags
{
    int data;
    struct List_Tags *pNext;
}List_T;
void DeleteList(List_T* pHead)
{
    while(pHead)
    {
        List_T* tmp = pHead->pNext;
        delete pHead;
        pHead = tmp;
    }
}

So if you delete the list like this, wouldn't there be many wild pointers?
Posted
Updated 17-Dec-19 3:57am
v3

Pointer p in the first example dangles, as already explained.

Function DeleteList() is fine internally, but it still could be problematic, because the caller still holds a non-null pointer to the head of the deleted list, which now points into invalidated memory:
C++
void foo() {
    List_T* my_list = new List_T;
    my_list->data = 42;
    my_list->pNext = nullptr;
    if (my_list)
        std::cout << my_list->data; // prints 42
    DeleteList(my_list);
    if (my_list)
        std::cout << my_list->data; // undefined behaviour
}

If you want to clean that pointer too, you have to pass it by reference and set it to 0:
C++
void DeleteList(List_T*& pHead)
{
    while(pHead)
    {
        List_T* tmp = pHead->pNext;
        delete pHead;
        pHead = tmp;
    }
    pHead = nullptr;
}

With this change the function foo() above will not try to access the invalidated pointer.
 
Share this answer
 
p is indeed a wild pointer because it points to space that has been freed (through the p2 pointer).
However the code for deleting a linked list is quite OK. tmp contains all the time a valid pointer to the next element in the list. The first element in the list, pointed by pHead, is freed and immediately after the next element becomes the head.
 
Share this answer
 
Comments
Member 13729319 16-Dec-19 22:07pm    
Code as :
DeleteList(pList);

But isn't function DeleteList the same as deleting only P2? Will variable pList become a wild pointer?
Rick York 17-Dec-19 1:28am    
No, DeleteList traverses the list and deletes all objects allocated. pList will point to data that has been freed and it was not nulled so, yes.
You are right, pList becomes a wild pointer. But in that sense every pointer becomes wild when the object it points to gets deallocated.

As Stefan suggested, pHead can passed as a reference to a pointer so when DeleteList returns pHead is NULL. A null pointer is still a wild pointer (points to an invalid address) but I'd call it a "controlled wild pointer" because you can test it to see if it's valid. Compare it with the situation of p where you cannot know if it's pointing it to a valid block or not.

Another solution is to avoid working with pointers and deallocate space only when the object goes out of scope (in destructor). Depending on your problem this doesn't always work.
 
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