尝试通过内存中的地址调用函数时C++程序崩溃(如何修复?)

Nul*_*ptr 0 c++ memory pointers function call

我试图调用函数testaddresscall(),其定义如下:

void testaddresscall()
{
   printf("success");
}

int main(void)
{
   void(*testaddresscallfunc)(void);
   testaddresscallfunc= &testaddresscall;
   cout << *testaddresscallfunc; //it printed 0x012D2050

   typedef void testfunc(void);
   testfunc* callbyaddress = (testfunc*)0x012D2050;
   callbyaddress();
}
Run Code Online (Sandbox Code Playgroud)

然后发生这种情况

test.exe中0x012D2050处的未处理异常:0xC0000005:访问冲突执行位置0x012D2050.

Bad*_*Zen 5

不要那样做.每次运行代码时,函数的地址都会改变.

  • 您不理解该选项(顺便说一下,它仅存在于您在Windows上使用的特定编译器中,VC).DYNAMICBASE:NO构建一个在运行时不会布局随机化的对象.这是一项增加安全性并使病毒作者受挫的功能.它不保证在调用之间修复布局.如果您以简单的方式更改代码,您肯定无法使用相同的布局 - 例如添加/删除函数,变量等等. (2认同)