当前位置:网站首页>Field pointer? Dangling pointer? This article will help you understand!

Field pointer? Dangling pointer? This article will help you understand!

2021-08-07 23:56:38 Linux ape


author :Linux The ape

brief introduction :CSDN Blogger ,C/C++、 interview 、 Brush problem 、 Just consult me , Pay attention to me , Private chat with questions !

Focus on Columns :C/C++ Interview customs collection  ( High quality articles are constantly updated ……)


Catalog

One 、 Wild pointer

Two 、 Dangling pointer

2.1 Situation 1

2.2 Situation two

2.3 Situation three


Wild pointer and dangling pointer are two common concepts in pointer , This article will explain with examples .

One 、 Wild pointer

A wild pointer is a pointer that has not been initialized , Neither points to legal memory space , Not used NULL/nullptr Initialization pointer .

Let's take a simple example :

#include <iostream>
using namespace std;

int main()
{
    int *p;     //  Wild pointer 
    int *q = NULL; //  Non wild pointer 
    p = new int(5);  // p  Now it's no longer a wild pointer 
    q = new int(10); 
    cout<<"*p = "<<*p<<endl;
    cout<<"*q = "<<*q<<endl;
    free(p);
    free(q);
    return 0;
}

The output is :

linuxy@linuxy:~/wildPointer$ ./main 
*p = 5
*q = 10
linuxy@linuxy:~/wildPointer$ 

p There is no initial value when defining , This is the wild pointer .

Two 、 Dangling pointer

Dangling pointer refers to The memory space pointed to by the pointer has been freed or is no longer valid .

There are three situations where dangling pointers occur , Let's take a look at... With examples :

2.1 Situation 1

After releasing the pointer resource , Before assignment again .

#include <iostream>
using namespace std;

int main()
{
    int *p = new int(5);
    cout<<"*p = "<<*p<<endl;
    free(p);  // p  When released, it becomes a dangling pointer 
    p = NULL; //  Non dangling pointer 
    return 0;
}

p The pointer is being free after , Become a dangling pointer , By NULL After assignment, it is no longer a dangling pointer .

Be careful : here free What fell was p Of memory space , It's not a variable p, Take a look at :

#include <iostream>
using namespace std;

int main()
{
    int *p = new int(5);
    cout<<"*p = "<<*p<<endl;
    cout<<"p  Address :"<<p<<endl;
    free(p);  // p  When released, it becomes a dangling pointer 
    cout<<"p  Address :"<<p<<endl;
    cout<<"*p = "<<*p<<endl;
    p = NULL; //  Non dangling pointer 
    return 0;
}

The output is :

linuxy@linuxy:~/wildPointer$ ./main 
*p = 5
p  Address :0x55a885ef6eb0
p  Address :0x55a885ef6eb0
*p = 0
linuxy@linuxy:~/wildPointer$ 

You can see , free Before and after p Your address is unchanged ,free What's released is p Memory space pointed to , After release, it indicates that the fast memory can be reallocated , as for free after *p Value , Depending on the compiler .

2.2 Situation two

Out of the scope of the variable .

#include <iostream>
using namespace std;

int main()
{
    int *p;
    {
        int tmp = 10;
        p = &tmp;
    }
    //p  Become a dangling pointer here 
    return 0;
} 

In the above example , Variable tmp The scope of action is the nearest layer of parentheses , The reference outside the parentheses is beyond the scope of the variable .

2.3 Situation three

Points to the function local variable .

#include <iostream>
using namespace std;

int* getVal() {
    int tmp = 10;
    return &tmp;
}

int main()
{
    int *p = getVal(); // Dangling pointer 
    cout<<"*p = "<<*p<<endl;
    return 0;
}

In function getVal After the execution , The memory space of local variables will be released , And here p Points to the local variable in the function ,p It becomes a dangling pointer , Can be tmp Turn into static Of .

Good article recommends

【 Ten thousand word arrangement 】️8 Big sorting algorithm ️【 Recommended collection 】

️ One article mastery C/C++ Memory leak , Prevent memory leaks and detection tools !️

3 Minutes to understand the difference between pointers and references 【️ Recommended collection ️】

【C/C++ For the interview 】 The difference between declaration and definition


Welcome to my friends 、 Collection 、 Leaving a message.


版权声明
本文为[Linux ape]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/08/20210807235532129X.html

随机推荐