在最近的一道测试题中,我被要求打印以下程序的输出。我得到了正确的答案,然而,这个程序给我带来了巨大的精神痛苦,因为我不知道在写入超出数组界限的内存时会有什么行为。
以下是正在讨论的程序,注释是我的注释:
#include <stdio.h>
#define MAX 4
void RecordArgs(int x);
int main()
{
RecordArgs(1);
RecordArgs(7);
RecordArgs(-11);
return 0;
}
void RecordArgs(int x)
{
static int i = 0;
int call_count = 0;
int arg_history[MAX] = {0};
if (call_count == MAX)
{
# call_count is not static and is initialized to 0 on each call
# as a result, under no circumstance can call_count == MAX and
# this printf is never executed
printf("Too many calls to RecordArgs\n");
}
else
{
# index out of bounds on second/third call (i + call_count will be 4??)
arg_history[i + call_count] = x;
++call_count;
++i;
for (i = 0; i < MAX; ++i)
printf("%d ", arg_history[i]);
printf("\n");
}
}和预期输出:
1 0 0 0
0 0 0 0
0 0 0 0当第二次和第三次调用RecordArgs时,7和-11值写在哪里?我试着在不同的设置下编译它,看看我是否能让它写两次不应该写的东西,但我尝试过的每一件事都导致了准确的输出,没有任何段错误。
发布于 2013-04-19 09:16:51
在Patashu的评论基础上展开,当你从一个页面访问内存的方式与该页面的内存权限冲突时,就会发生segmentation faults。换句话说,当您以不允许的方式访问一页内存时,就会发生这种情况。在您的情况下可能发生的情况是,您正在访问的内存仍然在存储arg_history的同一个页面内,您显然有权对其进行读写。
另一种可能的情况是,紧跟在您正在处理的内存页之后的内存页具有相同的权限,允许您以相同的方式访问它。
在任何情况下,这在C中都是未定义的行为。尽管您看到了“预期结果”,但这并不意味着程序是正确的。事实上,在这种情况下,越界错误可能不会被注意到,如果它不会导致分段错误的话。
https://stackoverflow.com/questions/16095614
复制相似问题