-2
我對這個概念很新穎,我很困惑,如果一個懸掛指針是指向指向已釋放或刪除的內存的內存位置的指針,那麼在這種情況下爲什麼它仍然可以調用功能test()
懸掛指針仍然訪問內存值
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
class MyClass{
public:
void test(){
cout<< "just checking"<<endl;
}
};
int main(int argc, char **argv)
{
MyClass *p (new MyClass());;
MyClass *q = p;
delete p;
q->test();
p = NULL;
q->test();
return 0;
}
任何幫助,將不勝感激。
您正在調用[未定義行爲](https://en.wikipedia.org/wiki/Undefined_behavior)。這意味着編譯器可以自由地生成任何希望的代碼,包括看起來可行的代碼。 –
這個問題與C完全無關。 –
如果你在Windows上,更容易發現懸空指針 - 在調試模式下,Visual Studio用0xCD字節填充釋放的內存。 – rustyx