c++野指针

2022-11-07  本文已影响0人  arkliu

野指针分类

int *p;
cout << "p = "<<p <<"  *p = "<<(*p)<<endl;
int *p = new int(3);
cout << "p = "<<p <<"  *p = "<<(*p)<<endl;
delete p;
cout << "p = "<<p <<"  *p = "<<(*p)<<endl;
image.png
#include <iostream>
#include<string>
using namespace std;

int* fun() {
    int a = 3;
    cout << "a = "<<a <<"  &a = "<<&a<<endl;
    return &a;
}

int main() {
    int *p = fun();
    cout << "p = "<<p <<"  *p = "<<(*p)<<endl;
    return 0;
}
image.png

规避野指针

上一篇下一篇

猜你喜欢

热点阅读