473,657 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoiding dereference of NULL pointer

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!

Dec 15 '07 #1
17 6354
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
Dec 15 '07 #2
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...)

Dec 15 '07 #3
On Dec 15, 10:52 am, Tony Jackson <tj76...@DELETE THIS.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.
Dec 15 '07 #4
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.

Dec 15 '07 #5
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
correspondin g 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
Dec 15 '07 #6
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.

Dec 15 '07 #7
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
Dec 15 '07 #8
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

Dec 15 '07 #9

"Chris Dollin" <eh@electriched gehog.netwrote 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

Dec 15 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
9694
by: Joe Seigh | last post by:
Is there a good write on this. The textbooks I have fluff over on this? Specifically, I trying to dereference with 2 levels of type conversion not 1, i.e. X<T> -> z => Y<T> -> z => T* -> z and *X<T> => *Y<T> => *T The Y<T> conversion has to be done as an expression temp. It cannot be done
10
26021
by: Denis Palmeiro | last post by:
Hello eveyone. What exactly is a NULL Pointer Dereference? I tried searching google but the only results that returned were bug reports. Some example code, if possible, would also be appreciated. Thanks in advance. -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
6
5404
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists, vectors, operator overloading and what not.. And i was wondering if there is a way to override the global dereference operator, so to be able to check the address that one tries to dereference. This gives the ability to throw an exception when...
14
3014
by: mast2as | last post by:
Hi everyone, I am trying to implement some specs which specify that an array of parameter is passed to a function as a pointer to an array terminated by a NULL chatacter. That seemed fairly easy to implement. I had a special Malloc function that would allocated the number of needed bytes for the objects i needed to store + 1 additional byte to save the NULL character /*!
30
2942
by: somenath | last post by:
Hi All, I have one doubt regarding pointer .I have one small code as mentioned bellow . #include<stdio.h> #include<stdlib.h> int main (void) { int *p; int *ptr= malloc(sizeof(int *));
2
2405
by: andrey.vul | last post by:
code: #include <iostream> #include <vector> #include <stdexcept> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring>
26
4500
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Do you think we can reach any kind of consensus on whether the following code's behaviour is undefined by the Standard? int my_array; int const *const pend = *(&my_array + 1); Considering the syntax of the language, then we definitely do dereference an invalid pointer... but if we consider the mechanics of the language, then we know that nothing "happens" when we dereference a pointer
8
2034
by: Rafael Anschau | last post by:
I read that you should assign null (0) to all pointers that you call delete on. Does that mean: *p=0(set the value pointed to to 0). or p=0(set the address held to zero).
0
8403
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.