What is a dangling pointer?

后端 未结 7 810
心在旅途
心在旅途 2020-11-22 02:48

I know this is pretty common question, but still new for me!

I don\'t understand concept of dangling pointer, was googling around, and writing test methods to find o

相关标签:
7条回答
  • 2020-11-22 03:54
    
    //Declaring two pointer variables to int
    int * ptr1;
    int * ptr2;
    // Allocating dynamic memory in the heap
    ptr1 = new int;
    ptr2 = ptr1; // Having both pointers to point same dynamic memory location
    //deleting the dynamic memory location 
    delete ptr1;
    ptr1 = nullptr; 
    //ptr2 is still pointing the already deleted memory location 
    //We call ptr2 is a dangling pointer
    
    0 讨论(0)
提交回复
热议问题