+ Reply to Thread
Results 1 to 2 of 2

Thread: What is dangling pointer?

  1. #1

    What is dangling pointer?

    Hi,

    What is dangling pointer?

    I heard that they are dangerous.

    How to avoid them?

  2. #2
    Dangling pointer is a pointer that do not points to a valid pointer and it can cause in-defined or sometimes a crash.

    Below code shows the same:

    Code:
    void UseDanglingPointer()
    {
    	char *pDanglingPointer = new char[10] ;
    
    	delete []pDanglingPointer ;
    
    	//use pDanglingPointer now will crash the application!
    	//pDanglingPointer is dangling pointer
    
    	//Always pDanglingPointer to null after it gets deleted!
    	pDanglingPointer = NULL ;
    }
    Tips:
    a. Always check your pointer against NULL before use it.
    b. Always assign pointer to NULL after deletion.
    c. Always initialize pointer with NULL or by proper initialization code.

+ Reply to 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