为什么此代码实际上可靠地起作用,不是不稳定的,未定义的? 它取消引用了对象的未分配悬空指针。 谢谢。
#include <iostream>
using namespace std;
class Base{
public:
void vf()
{
cout<<"vf\n";
}
void pr(){
cout<<"vb\n";
}
};
int main() {
Base* b ;
//Base * bp = new Base; //trying to disrupt memory
char ar[]= "ttttttttttt"; //trying to disrupt memory
b->pr();
char aa[]= "ttttttttttt";
b->vf();
return 0;
}
欢迎来到未定义行为的奇妙世界!根据C ++规范,此问题的行为是不确定的,因此您看到的内容可能在您的系统上起作用,但在其他系统上崩溃,反之亦然。
实际上,这里发生的事情可能是编译器如何为成员函数生成代码的产物。通常,成员函数如下所示:
可能会像是一个自由函数一样被编译:
从这个意义上说,当你写
编译器将其视为已编写
and nothing bad happens, because
Base_doSomething
doesn't reference thethis
pointer, which is where all the bad things happen.Now, this is very brittle. If you try to read any of the data members of the
Base
type, this code will crash because then you are reading from a bad pointer. Similarly, ifdoSomething
were avirtual
function, you'd get a crash because calling a virtual function (typically) requires reading from the receiver object to find the vtable to determine which function to call, and a dangling pointer will then lead to a bad memory access.因此,总结一下:
bp->doSomething()
looks like a pointer dereference, it might not actually involve a pointer dereference.this
" pointer passed as a first argument.希望这可以帮助!