为什么不取消引用null会使我的程序崩溃? [英] Why doesn't dereferencing null crash my program?

查看:80
本文介绍了为什么不取消引用null会使我的程序崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码来引发崩溃(我正在测试一些

I wrote the following code to induce a crash (I was testing out some issues with CrashReporter):

int *nullp = NULL;
int val = *nullp;    
NSLog(@"Hello world %d", val);

在一个全新的项目中(我将其放在应用程序委托的applicationDidFinishLaunching:中),它按预期崩溃.但是,当我将其添加到现有项目之一时,它不会崩溃!实际上,它最终会在系统日志中打印"Hello world 0".

In a brand new project (I put it in the app delegate's applicationDidFinishLaunching:) it crashes as expected. But when I add it to one of my existing projects, it doesn't crash! In fact, it ends up printing "Hello world 0" to the system log.

这对我来说没有任何意义.为什么空取消引用不会导致崩溃?

This doesn't make any sense to me. Why doesn't the null dereference cause a crash?

推荐答案

取消引用NULL具有未定义的行为,它不一定会导致崩溃.

Dereferncing NULL has undefined behavior, it doesn't have to cause crash.

这就是说,如果在两个项目中有不同的编译器选项,则很有可能在这些情况下获得不同的行为.有关未定义的行为,请参见此 LLVM博客文章.

That being said, if you have different compiler options in the two project you have a good chance to get different behavior for those cases. See this LLVM blog post about undefined behaviors.

这篇关于为什么不取消引用null会使我的程序崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆