| Re: Non-null pointers in C/C++? |
|
 |
|
 |
|
 |
|
 |
Group: comp.lang.c.moderated · Group Profile
Author: Wojtek LerchWojtek 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.
>> -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.
No, but there may be optimization opportunities that are impossible to
detect without knowing that a pointer cannot possibly be null, and quite
often a compiler has no way to know that, especially if pointers are passed
around between separately compiled translation units.
--
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.
|