C++内联函数抛出异常:运行时检查失败 #0 - ESP 的值不正确
我正在编写一些内联函数来娱乐,它抛出了一个我以前从未遇到过的异常。有趣的是,如果我继续,在异常刚刚停止程序的执行流程后,它将返回两个整数的和。
__declspec(dllexport) int addintegers(int one, int two)
{
int answer = 0;
__asm
{
mov eax, 0
push two
push one
call add
mov answer, eax
}
return answer;
} // Debugger stops here with exception message
异常消息:
运行时检查失败 #0 - ESP 的值未在函数调用中正确保存。这通常是用一种调用约定声明的函数和用另一种调用约定声明的函数指针调用的结果。
// add function definition
int add(int one, int two)
{
return one + two;
}
I'm writing some inline functions for fun and it throws an exception I have never encountered before. The funny thing is, that if I continue, after the exception just stopped the flow of execution of my program, it will return the sum of two integers.
__declspec(dllexport) int addintegers(int one, int two)
{
int answer = 0;
__asm
{
mov eax, 0
push two
push one
call add
mov answer, eax
}
return answer;
} // Debugger stops here with exception message
Exception Message:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
// add function definition
int add(int one, int two)
{
return one + two;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对汇编程序了解不多,并且您没有向我们展示
add()
的声明,但如果它遵循 C 的调用约定,则必须在调用后从堆栈中弹出参数返回给调用者。要求调用者(而不是被调用者)清理堆栈,使得 C 能够拥有参数数量可变的函数,例如
printf()
。I don't know much about assembler, and you don't show us the declaration of
add()
, but if it adheres to C's calling convention you have to pop the arguments from the stack after the call returned to the caller.Requiring the caller to clean up the stack, rather than the callee, is what allows C to have functions with a variable number of arguments, like
printf()
.