在 C++ 中,堆栈会以何种方式被破坏?我猜想的一种方法是通过访问超出其边界的数组来覆盖堆栈变量。还有其他方法可以使其损坏吗?
但是这些并不是 C++ 所特有的,它对堆栈没有任何概念。
违反单一定义规则可能会导致堆栈损坏。下面的例子看起来很愚蠢,但我已经见过几次用不同的配置编译不同的库了。
struct MyStruct
{
int val;
#ifdef LARGEMYSTRUCT
char padding[16];
#endif
}
#define LARGEMYSTRUCT
#include "header.h"
//Here it looks like MyStruct is 20 bytes in size
void func(MyStruct s)
{
memset(s.padding, 0, 16); //corrupts the stack as below file2.cpp does not have LARGEMYSTRUCT declared and declares Mystruct with 4 bytes
return; //Will probably crash here as the return pointer has been overwritten
}
#include "header.h"
//Here it looks like MyStruct is only 4 bytes in size.
extern void func(MyStruct s);
void caller()
{
MyStruct s;
func(s); //push four bytes on to the stack
}
获取指向堆栈变量的指针是一个好方法:
void foo()
{
my_struct s;
bar(&s);
}
如果 bar 保留指针的副本,那么将来任何事情都可能发生。
总结:当有杂散指针指向堆栈时,就会发生堆栈损坏。
C++ 标准没有定义栈/堆。此外,有多种方法可以在程序中调用未定义的行为——所有这些都可能会损坏您的堆栈(毕竟是 UB)。简短的回答是——你的问题太模糊,无法给出有意义的答案。
使用错误的调用约定调用函数。
(虽然这在技术上是特定于编译器的,而不是 C++ 的问题,但每个 C++ 编译器都必须处理这个问题。)
在析构函数内抛出异常是一个不错的选择。它会弄乱堆栈展开。