避免取消引用NULL指针 [英] Avoiding dereference of NULL pointer

查看:67
本文介绍了避免取消引用NULL指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我对C编程很陌生 - 我有更多的Java背景:也许

有人可以告诉我。


我几乎完成了一个小程序,但它有一些难以发现的错误

基本上会导致一些指针有时在NULL

错误的时间。当它试图将b
取消引用NULL时,程序就会爆炸,这显然不是很好(特别是当它与客户一起发生时,b $ b发生了!) !)。


我不认为有任何严重的问题需要担心,而且我想要做的是b $ b喜欢做的事情是能够忽略取消引用尝试并且只需携带

- 可能在下一次循环时指针就可以了。在
Java中,我在主循环周围放了一个try块,如果我们遇到一个

异常,那么就跳回到主循环的开头。什么是C中相应的东西?


谢谢!

解决方案

Tony Jackson写道:


>




我对C编程很陌生 - 我有更多的Java背景:也许

有人在这里可以建议我。


我差不多了完成一个小程序,但它有一些难以发现的错误

,这基本上导致一些指针有时在

错误的时间为NULL。当它试图将b
取消引用NULL时,程序就会爆炸,这显然不是很好(特别是当它与客户一起发生时,b $ b发生了!) !)。


我不认为有任何严重的问题需要担心,而且我想要做的是b $ b喜欢做的事情是能够忽略取消引用尝试并且只需携带

- 可能在下一次循环时指针就可以了。在
Java中,我在主循环周围放了一个try块,如果我们遇到一个

异常,那么就跳回到主循环的开头。

在C中,

对应的东西是什么?



放置代码以防止空指针的干扰尝试




-

pete


放置代码以防止


相反,空指针的deference尝试。



感谢您的回复。这意味着在每个指针上测试NULL值

dereference这将是很多额外的代码 - 我正在寻找一个

catch-all解决方案。 (不幸的是,我已经经历过几个不同的地方,程序试图取消引用NULL,以及谁知道
知道有多少其他人潜伏在后台...... )


12月15日上午10:52,Tony Jackson< tj76 ... @ DELETETHIS.gmail.com>

写道:




我几乎完成了一个小程序,但它有一些难以发现的错误

基本上导致一些指针有时在

错误的时间为NULL。当它试图将b
取消引用NULL时,程序就会爆炸,这显然不是很好(特别是当它与客户一起发生时,b $ b发生了!) !)。



使用断言来捕获在调试期间永远不会为空的NULL指针

(即表示程序逻辑损坏),然后修复

代码。其他任何东西都是糟糕的黑客。


对于可能为null的指针,只需通过比较

与预处理器宏NULL来检查是否为空。


Hi

I''m quite new to C programming - I have more of a Java background: maybe
someone here can advise me.

I''m nearly finishing a little program but it has some hard-to-find bugs
that basically lead to a couple pointers sometimes being NULL at the
wrong time. As it stands the program just bombs out when it tries to
dereference the NULL, which obviously isn''t good (especially when it
happens with the customer looking on!!).

I don''t think there''s any serious problem to worry about, and what I''d
like to do is be able to ignore the dereference attempt and just carry
on - probably the next time round the loop the pointer will be OK. In
Java, I''d put a try block around the main loop, and if we catch an
exception then just jump back to the start of the main loop. What''s the
corresponding thing in C?

Thanks!

解决方案

Tony Jackson wrote:

>
Hi

I''m quite new to C programming - I have more of a Java background: maybe
someone here can advise me.

I''m nearly finishing a little program but it has some hard-to-find bugs
that basically lead to a couple pointers sometimes being NULL at the
wrong time. As it stands the program just bombs out when it tries to
dereference the NULL, which obviously isn''t good (especially when it
happens with the customer looking on!!).

I don''t think there''s any serious problem to worry about, and what I''d
like to do is be able to ignore the dereference attempt and just carry
on - probably the next time round the loop the pointer will be OK. In
Java, I''d put a try block around the main loop, and if we catch an
exception then just jump back to the start of the main loop.
What''s the
corresponding thing in C?

Put code in place to prevent
the deference attempt of the null pointer, instead.

--
pete


Put code in place to prevent

the deference attempt of the null pointer, instead.

Thanks for the reply. That would mean testing for NULL at every pointer
dereference which would be a lot of extra code - I was looking for a
catch-all solution. (Unfortunately I''ve already experienced several
different places where the program tries to dereference NULL, and who
knows how many others there are lurking in the background...)


On Dec 15, 10:52 am, Tony Jackson <tj76...@DELETETHIS.gmail.com>
wrote:

Hi
I''m nearly finishing a little program but it has some hard-to-find bugs
that basically lead to a couple pointers sometimes being NULL at the
wrong time. As it stands the program just bombs out when it tries to
dereference the NULL, which obviously isn''t good (especially when it
happens with the customer looking on!!).

Use assertions to catch the NULL pointers that should never be null
(i.e. indicating broken program logic) during debugging, then fix your
code. Anything else is a bad hack.

As for pointers which may be null, just check for nullity by comparing
against the preprocessor macro NULL.


这篇关于避免取消引用NULL指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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