分段错误和分段错误核心转储之间的区别
考虑以下 C 代码,
int n;
scanf("%d",n)
它会给出错误 Segmentation failure core dumped in GCC compiler in Linux Mandriva
但以下代码
int *p=NULL;
*P=8;
仅给出分段错误 为什么会这样..?
consider the following code in C
int n;
scanf("%d",n)
it gives the error Segmentation fault core dumped in GCC compiler in Linux Mandriva
but the following code
int *p=NULL;
*P=8;
gives only segmentation fault why is that so..?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
核心转储是一个文件,其中包含程序崩溃时的状态和内存转储。由于核心转储会占用大量磁盘空间,因此它们的大小有一个可配置的限制。您可以使用
ulimit -c
查看它。现在,当出现分段错误时,默认操作是终止进程并转储核心。您的 shell 会告诉发生了什么,如果进程因分段错误信号而终止,它将打印分段错误,并且如果该进程另外转储了核心(当 ulimit 设置时)并且生成核心转储的目录的权限允许),它会告诉你这一点。
A core dump is a file containing a dump of the state and memory of a program at the time it crashed. Since core dumps can take non-trivial amounts of disk space, there is a configurable limit on how large they can be. You can see it with
ulimit -c
.Now, when you get a segmentation fault, the default action is to terminate the process and dump core. Your shell tells what has happened, if a process has terminated with a segmentation fault signal it will print
Segmentation fault
, and if that process has additionally dumped core (when theulimit
setting and the permissions on the directory where the core dump is to be generated allow it), it will tell you so.假设您在同一系统上使用相同的 ulimit -c 设置(这将是我对您所看到的差异的第一个猜测),那么优化器可能是“注意到”第二个示例中明显未定义的行为,并生成自己的退出。您可以使用 objdump -x 进行检查。
Assuming you're running both of these on the same system, with the same
ulimit -c
settings (which would be my first guess as to the difference you're seeing), then its possible the optimizer is "noticing" the clearly undefined behavior in the second example, and generating its own exit. You could check withobjdump -x
.在第一种情况下,“n”可以具有任何值,您可能拥有该内存(或不拥有),它可能是可写的(或不可写),但它可能存在。 n 没有理由一定为零。
写入 NULL 绝对是顽皮的行为,操作系统会注意到这一点!
In the first case 'n' could have any value, you might own this memory (or not), it might be writeable (or not) but it probably exists. There is no reason that n is necessarily zero.
Writing to NULL is definetly naughty and something the OS is going to notice!