本文介绍了取消引用空指针(C/C ++)时可以保证segfault的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的C/C ++代码中

In the C/C++ code below

int * a = malloc(N * sizeof(int)); // suppose return value is NULL
a[2] = 1;

在malloc返回 NULL 的情况下,我是否保证将发生segfault或行为不可预测?

In the case where malloc returns NULL, do I have a guarantee that segfault will occur or is the behavior unpredictable?

推荐答案

总之,没有.

要引用 Wikipedia :

在C中,取消引用空指针的行为是未定义.

In C, the behavior of dereferencing a null pointer is undefined.

还要查看此取消引用的空类指针的野生示例,但仍然可以正常工作.

Also check out this wild example of a null class pointer dereferenced, but which still works just fine.

基本上,不要这样做,但随后您知道了:)

Basically, don't do this, but then you knew that :)

这篇关于取消引用空指针(C/C ++)时可以保证segfault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 06:42
查看更多