分段错误和分段错误核心转储之间的区别

发布于 2024-12-11 13:18:03 字数 231 浏览 0 评论 0原文

考虑以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身软味 2024-12-18 13:18:03

核心转储是一个文件,其中包含程序崩溃时的状态和内存转储。由于核心转储会占用大量磁盘空间,因此它们的大小有一个可配置的限制。您可以使用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 the ulimit setting and the permissions on the directory where the core dump is to be generated allow it), it will tell you so.

_蜘蛛 2024-12-18 13:18:03

假设您在同一系统上使用相同的 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 with objdump -x.

世界等同你 2024-12-18 13:18:03

在第一种情况下,“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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文