Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Avoiding dereference of NULL pointer

Reply
Thread Tools

Avoiding dereference of NULL pointer

 
 
Tony Jackson
Guest
Posts: n/a
 
      12-15-2007
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!

 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      12-15-2007
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
 
Reply With Quote
 
 
 
 
Tony Jackson
Guest
Posts: n/a
 
      12-15-2007
> 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...)

 
Reply With Quote
 
jkohen@gmail.com
Guest
Posts: n/a
 
      12-15-2007
On Dec 15, 10:52 am, Tony Jackson <(E-Mail Removed)>
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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      12-15-2007
pete wrote, On 15/12/07 09:59:
> 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!!).


Well, the program is not ready to put in front of a customer if you do
not understand *why* the pointers are null.

>> I don't think there's any serious problem to worry about,


In that case I am glad that I am not your customer.

>> 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.


Or, as you seem to not understand what is going on, something else that
does not show up as easily might be wrong.

>> 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.


Or better yet actually find out why it is null and fix the real cause.
--
Flash Gordon
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      12-15-2007
Tony Jackson wrote:

>> 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...)


A short while back Martin Ambuhl posted an illustration of handling a
SIGSEGV signal (which is what is likely to be delivered to your program
on a null pointer deference) and continuing. Be aware though that the C
standard explicitly says that the further behaviour of a program that
handles SIGSEGV is undefined, so anything may happen. Your
implementation may, however provide more guarantees.

Your comments indicate that the code is broken, to say the least. I'm
not sure about Java, but in C broken code generally leads to nasty
surprises. Consider handling SIGSEGV only if a better option (like
correcting the code in the first place) fails. Signal handling is
subtle and difficult to get right in C.

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      12-15-2007
Tony Jackson wrote:

> 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.


(and then he said)

> 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.


In Java, as in C, if our program dereferences a null, /it is an error/,
and the program should be fixed. Yes, it's nice that you can catch the
error in Java -- but you should /still/ work out /why/ some idiot code
is dereferencing null, and sort it out.

I've written a fair bit of Java, and blithely ignoring null pointer
exceptions wasn't part of /any/ of it.

Exception handling is for robustness, not wimping out. How on Earth
do you know the program's in a safe state if it's been arbitrarily
thrown out of by an NPE?

--
Non-Null Hedgehog
Nit-picking is best done among friends.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-15-2007
Tony Jackson wrote:
>
> I'm quite new to C programming - I have more of a Java background: maybe
> someone here can advise me.


Yes. Try to write accurate code.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-15-2007
Tony Jackson wrote:
>> 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. ...


Properly designed code automatically ensures that pointers are not null
when de-referenced; there's no need to insert large numbers of explicit
tests for NULL except during debugging, when you're trying to figure out
the way in which your code is improperly designed.

Your code is obviously not properly designed in this regard, and you
therefore DO need to debug it. I guarantee you that the kind of mistakes
that result in attempts to dereference null pointers will not be minor,
ignorable ones. Even if you could ignore such attempts, your program
will go ahead and NOT do what you intended it to do. Fix the problems
that cause such attempts to happen.

> ... (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...)


I would strongly recommend not using your program until you have found
enough time to find and fix all of the bugs that
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      12-15-2007

"Chris Dollin" <(E-Mail Removed)> wrote in message
> Tony Jackson wrote:
>
>> 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!!).

>
> Exception handling is for robustness, not wimping out. How on Earth
> do you know the program's in a safe state if it's been arbitrarily
> thrown out of by an NPE?
>

The customer isn't necessarily very sophisticated. He wants to see a program
doing things on screen, so he's got confidence that the project is
progressing to schedule. If the program bombs out with a segfault he
interprets that as code in a very early stage of development. If it just
produces a garbage answer but in a nice-looking dialogue box, he interprets
that as a bit of a glitch that can be fixed.

Whilst in the long run you have to get to the root of the problem, we don't
all have the luxury of ignoring short term pressures.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to pointer intialize to NULL but still point to NULL Christopher C++ 4 07-09-2011 12:35 AM
How bad it is to dereference a null pointer Armen Tsirunyan C++ 18 10-05-2010 08:17 AM
Null pointer (NULL array pointer is passed) aneuryzma C++ 3 06-16-2008 05:48 AM
vector has segfault null dereference andrey.vul@gmail.com C++ 6 10-03-2007 01:45 AM
NULL Pointer Dereference Denis Palmeiro C Programming 10 07-16-2003 12:33 PM



Advertisments