我有类似于下面的代码。
int foo (unsigned int a)
{
struct tid{
int id;
char abd[8];
};
int ret = 0;
struct tid arr[256];
ret = arr[a];
...
}
传递给 foo 的 a 值为 0xFFFFFFFF。现在,由于我们正在访问堆栈之外的内存位置,我认为系统会出现故障,但事实并非如此。 我想知道我可以朝哪个方向找出它是如何运作的。 所考虑的机器是 MIPS 4K,具有 32 位寻址和 256 MB RAM。我正在使用 gcc 在 x86 PC 上交叉编译代码。
最佳答案
Now, since we are accessing a memory location outside of the stack, I would assume the system would fault, but it is not.
在堆栈上分配的内存范围之外写入是 undefined behaviour 。调用这种未定义的行为通常(但并非总是)会导致段错误。 但是,您无法确定是否会发生段错误。
维基百科链接对此进行了很好的解释:
When an instance of undefined behavior occurs, so far as the language specification is concerned anything could happen, maybe nothing at all.
因此,在这种情况下,您可能会遇到段错误,程序可能会中止,或者有时它可能会正常运行。或者,任何东西。无法保证结果。
I would like to know a direction in which I could look to find out how this could work.
我不知道你在这里问什么。您能澄清一下“工作”的含义吗?你想要这个程序做什么?
关于c - 大批。访问 0xFFFFFFFF 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18681737/