c - 关于指针和分段错误
问题描述
所以,我有这个代码:
#include <stdio.h>
int main()
{
char *p;
int var = -1;
int i;
/* Printing all memory we can read */
p = (char *)&var;
/* No stopping condition. The OS will stop us */
for (i = 0;; i++)
{
printf("(%i) %i\n", i, p[i]);
/*
* The next statement also tries to write a zero
* starting from &var. Try to uncomment and explain ...
*/
//p[i] = 0;
}
}
以这种方式,它应该是分段错误,输出:
..................
(6556) 108
(6557) 116
..................
(6582) 0
(6583) 0
Segmentation fault
取消注释上面的这行代码:p[i] = 0;
它不会出现分段错误,输出是这样的:
..........
(1) 0
(2) 0
(3) 0
(4) 4
(1) 0
(2) 0
(3) 0
(4) 4
(1) 0
(2) 0
(3) 0
(4) 4
(1) 0
^C
i have to interrupt it
不明白为什么会这样,而且看起来我没有增加
解决方案
以这种方式,它会出现分段错误,因为它应该是
它不是“应该”做任何特别的事情。你在做什么p[i]
时i >= sizeof(int)
是未定义的行为,任何事情都可能发生。
在上面取消注释这行代码:
p[i] = 0;
它不会出现分段错误
它仍然是未定义的行为。它不会导致分段错误这一事实并不意味着什么。我只能猜测的是,p[i] = 0
在某些时候最终会i
用 a覆盖的值0
,因此你最终会陷入无限循环。
无法理解为什么会这样
试图理解未定义的行为没有多大意义。没有合理的解释,只能猜测。
如果您想讨论给定程序的特定编译器生成的程序集,那么我们可以讨论它并确切地告诉您事情发生的原因,但是鉴于您的 C 源代码,任何事情都可能发生。未定义的行为是未定义的行为。
推荐阅读
- node.js - 服务器上无法识别 Socket.io 语法
- angular - 编写验证器时出错
- go - 在模板中转义大括号
- mysql - 过滤没有子查询的值
- java - 获取 SWT 视图的大小
- google-data-studio - Google Data Studio 地理地图缩放区域的无效字段
- php - Laravel - 如何将项目从 zip 添加到供应商并使用它?
- postgresql - 限制 db 用户对谷歌云 sql 的权限
- git - 压扁的功能分支通过源树作为开发分支的一部分出现
- php - 在 Red Hat Linux 6 上使用 PHP odbc 连接到 Teradata 数据库时出错