Non-null pointers in C/C++?
  Home FAQ Contact Sign in
comp.lang.c.moderated only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c.moderated Profile…
 Up
Non-null pointers in C/C++?         


Author: angel_tsankov
Date: May 12, 2008 23:06

It's been some time since I started wondering why the C/C++ languages
do not have a non-null pointer (something like a copyable C++
reference)? In my opinion, using non-null pointers instead of the
"traditional" ones (wherever appropriate) would provide the following
benefits:
-allows restrictive programming techniques to be apllied at a lower
cost (e.g. if a pointer passed to a function may not be null, there
would be no need for a null-check before dereferencing it);
-allow for improvements in programming practices (having two types of
pointers will bring to attention the difference between them, namely
the possibility that "traditional" pointers can be null, hopefully
thus making it more obvious that "traditional" pointers should be
checked for null before thay are dereferenced);
-allow for more reliable code (having a "traditional" pointer where a
non-null pointer would do increases the risk of undefined behavior; on
the other hand, using a non-null pointer instead would be safer);
-provide compilers with further semantic information, thus enabling
them to perform more optimizations;
-make source code more concise (as a result of having fewer null-tests
when replacing "traditional" pointers with non-null ones). ...
Show full article (1.47Kb)
38 Comments
Re: Non-null pointers in C/C++?         


Author: Francis Glassborow
Date: May 15, 2008 09:00

angel_tsankov wrote:
> It's been some time since I started wondering why the C/C++ languages
> do not have a non-null pointer (something like a copyable C++
> reference)? In my opinion, using non-null pointers instead of the
> "traditional" ones (wherever appropriate) would provide the following
> benefits:
> -allows restrictive programming techniques to be apllied at a lower
> cost (e.g. if a pointer passed to a function may not be null, there
> would be no need for a null-check before dereferencing it);
> -allow for improvements in programming practices (having two types of
> pointers will bring to attention the difference between them, namely
> the possibility that "traditional" pointers can be null, hopefully
> thus making it more obvious that "traditional" pointers should be
> checked for null before thay are dereferenced);
> -allow for more reliable code (having a "traditional" pointer where a
> non-null pointer would do increases the risk of undefined behavior; on
> the other hand, using a non-null pointer instead would be safer);
> -provide compilers with further semantic information, thus enabling
> them to perform more optimizations;
> -make source code more concise (as a result of having fewer null-tests ...
Show full article (1.63Kb)
no comments
Re: Non-null pointers in C/C++?         


Author: Hans-Bernhard Bröker
Date: May 15, 2008 09:00

Note: I will ignore you mentioned C++ at all.

angel_tsankov wrote:
> It's been some time since I started wondering why the C/C++ languages
> do not have a non-null pointer (something like a copyable C++
> reference)?

Because it wouldn't work. Just because a pointer can't be NULL doesn't
mean it's safer to use. It could still be invalid in any number of
other ways.
> -provide compilers with further semantic information, thus enabling
> them to perform more optimizations;

There's no optimization you could do with a non-null pointer that you
would be forbidden to do with a NULL pointer --- optimizations are
already completely free to pick among various different kinds of
undefined behaviour. C code never has to check a pointer for NULL
before using it.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
no comments
Re: Non-null pointers in C/C++?         


Author: Gordon Burditt
Date: May 15, 2008 09:00

>It's been some time since I started wondering why the C/C++ languages
>do not have a non-null pointer (something like a copyable C++
>reference)? In my opinion, using non-null pointers instead of the
>"traditional" ones (wherever appropriate) would provide the following
>benefits:

So what's supposed to happen when the restriction is violated? And
are YOU willing to be aboard a plane running this code in the
automatic pilot when the restriction is violated? I can imagine
that it's really, really easy to have the function *definition*
declare the pointer arguments non-null but the declaration at the
point of use doesn't. Guess what the undefined behavior will do?
>-allows restrictive programming techniques to be apllied at a lower
>cost (e.g. if a pointer passed to a function may not be null, there
>would be no need for a null-check before dereferencing it);

Yes, there really is a need for a null-check. The question is what
to do (at runtime) when the check fails.
Show full article (2.49Kb)
no comments
Re: Non-null pointers in C/C++?         


Author: Eric Sosman
Date: May 22, 2008 14:51

angel_tsankov wrote:
> It's been some time since I started wondering why the C/C++ languages
> do not have a non-null pointer (something like a copyable C++
> reference)? In my opinion, using non-null pointers instead of the
> "traditional" ones (wherever appropriate) would provide the following
> benefits:
> -allows restrictive programming techniques to be apllied at a lower
> cost (e.g. if a pointer passed to a function may not be null, there
> would be no need for a null-check before dereferencing it);
> -allow for improvements in programming practices (having two types of
> pointers will bring to attention the difference between them, namely
> the possibility that "traditional" pointers can be null, hopefully
> thus making it more obvious that "traditional" pointers should be
> checked for null before thay are dereferenced);
> -allow for more reliable code (having a "traditional" pointer where a
> non-null pointer would do increases the risk of undefined behavior; on
> the other hand, using a non-null pointer instead would be safer);
> -provide compilers with further semantic information, thus enabling
> them to perform more optimizations;
> -make source code more concise (as a result of having fewer null-tests ...
Show full article (1.77Kb)
no comments
Re: Non-null pointers in C/C++?         


Author: Wojtek Lerch
Date: May 22, 2008 14:51

"Francis Glassborow" btinternet.com> wrote in message
news:clcm-20080515-0002@plethora.net...
> angel_tsankov wrote:
>> It's been some time since I started wondering why the C/C++ languages
>> do not have a non-null pointer (something like a copyable C++
>> reference)? [...]
> and what happens if a null pointer gets passed as an argument to a
> non-null pointer parameter?

It's undefined behaviour, but the compiler may be able to detect it and
issue a diagnostic. And in a debug mode, it could also insert opcodes to
test for null at runtime.

C99 actually has this kind of "non-null pointers" already, although they can
only be used for function parameters:

void fun( int ptr[ static 1 ] );
...
fun( NULL ); // undefined, easy to diagnose
Show full article (1.50Kb)
no comments
Re: Non-null pointers in C/C++?         


Author: Wojtek Lerch
Date: May 22, 2008 14:51

"Hans-Bernhard Bröker" wrote in message
news:clcm-20080515-0003@plethora.net...
> Note: I will ignore you mentioned C++ at all.
>
> angel_tsankov wrote:
>> It's been some time since I started wondering why the C/C++ languages
>> do not have a non-null pointer (something like a copyable C++
>> reference)?
>
> Because it wouldn't work. Just because a pointer can't be NULL doesn't
> mean it's safer to use. It could still be invalid in any number of other
> ways.

If the compiler knows that the pointer isn't supposed to be null, it can
help you detect some cases where you attempt to set it to null by mistake.
That makes it safer to use. Of course, your code can still be incorrect in
any number of other ways, but I don't think anybody has suggested that these
"non-null pointers" would be a panacea for all programming errors.
Show full article (1.71Kb)
no comments
Re: Non-null pointers in C/C++?         


Author: angel_tsankov
Date: May 22, 2008 14:51

On 15 Май, 19:00, Francis Glassborow
btinternet.com> wrote:
> angel_tsankov wrote:
>> It's been some time since I started wondering why the C/C++ languages
>> do not have a non-null pointer (something like a copyable C++
>> reference)? In my opinion, using non-null pointers instead of the
>> "traditional" ones (wherever appropriate) would provide the following
>> benefits:
>> -allows restrictive programming techniques to be apllied at a lower
>> cost (e.g. if a pointer passed to a function may not be null, there
>> would be no need for a null-check before dereferencing it);
>> -allow for improvements in programming practices (having two types of
>> pointers will bring to attention the difference between them, namely
>> the possibility that "traditional" pointers can be null, hopefully
>> thus making it more obvious that "traditional" pointers should be
>> checked for null before thay are dereferenced);
>> -allow for more reliable code (having a "traditional" pointer where a
>> non-null pointer would do increases the risk of undefined behavior; on
>> the other hand, using a non-null pointer instead would be safer);
>> -provide compilers with further semantic information, thus enabling ...
Show full article (1.86Kb)
no comments
Re: Non-null pointers in C/C++?         


Author: angel_tsankov
Date: May 22, 2008 14:51

On 15 Май, 19:00, Hans-Bernhard Bröker wrote:
> Note: I will ignore you mentioned C++ at all.
>
> angel_tsankov wrote:
>> It's been some time since I started wondering why the C/C++ languages
>> do not have a non-null pointer (something like a copyable C++
>> reference)?
>
> Because it wouldn't work.  Just because a pointer can't be NULL doesn't
> mean it's safer to use.  It could still be invalid in any number of
> other ways.
>
>> -provide compilers with further semantic information, thus enabling
>> them to perform more optimizations;
>
> There's no optimization you could do with a non-null pointer that you
> would be forbidden to do with a NULL pointer --- optimizations are
> already completely free to pick among various different kinds of
> undefined behaviour.  C code never has to check a pointer for NULL
> before using it. ...
Show full article (1.28Kb)
no comments
Re: Non-null pointers in C/C++?         


Author: angel_tsankov
Date: May 22, 2008 14:51

Obviously, I was not clear enough in my previous post.

First of all, I when I wrote 'restrictive programming' I meant
'protective programming', which I think is a nice 'thing'.

Secondly, I should say that a non-null pointer would probably be a new
(i.e. diferent) type and it would not be possible to convert a
'traditional' pointer to a non-null one. Right on the contrary, a non-
null pointer would be convertible to a 'traditional' one.

And last, a new operator might be needed - one that takes a single
argument and returns a non-null pointer to it. An implicit conversion
from an object to a non-null pointer would also do. Another
alternative is to change the semantics of the traditional address-of
(&) operator to return a non-null pointer (instead of a 'traditional'
one) and have an implicit conversion from a non-null pointer to a
'traditional' one (in order not to break existing code that uses the
address-of operator with 'traditional' pointers).

That's all for now. Hope this clarifies the idea somewhat.

P.S. A very short example usage (assuming that # designates a non-null
pointer and & returns a non-null pointer that is implicitly
convertible to a 'tarditional' one):
Show full article (2.07Kb)
no comments

RELATED THREADS
SubjectArticles qty Group
Re: iTunes non è una monnezza perchè non si può dire ma ad essere onesti...it.comp.macintosh ·
 
1 2 3 4