+ Reply to Thread
Results 1 to 3 of 3

Thread: What is dangling pointer?

  1. #1

    What is dangling pointer?

    What is dangling pointer?

    how to resolve issue related to it?

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    Dangling pointer is that we have a pointer and we already de-allocated or deleted it but still it points to the deleted memory location.

    When we try to use such deleted pointer it can cause a crash.

    Example:

    Code:
    void DanglingPointer()
    {
           int *p = new int ;
           delete p ;
    
           //if we use p then it can cause a crash...
    }
    
    void SolutionDanglingPointer()
    {
           int *p = new int ;
           delete p ;
           p = NULL ;
    
           //check p against NULL
           if (NULL != p)
           {
                  //use p here... it is safe now...
           }
    }

  3. #3
    That's great. Nice one.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts