Pointers vs. References

Bill Baxter dnewsgroup at billbaxter.com
Wed Jun 13 04:12:32 PDT 2007


Lionello Lunesu wrote:
> Michael Neumann wrote:
>> Synapse x = ...;
>> while (x != null)
>> {
>> }
>>
>> This segfaults, while:
>>
>> Synapse x = ...
>> while (!(x is null))
>> {
>> }
>>
>> does not.
> 
> That's not a bug.
> '==' and '!=' result in a call to opEquals, which is likely to segfault 
> when one of the operands is null. You should use 'is' or in your case 
> '!is'.
> 
> while (x !is null)
> {
> }
> 
> As for your question about references. A reference is basically the same 
> as a pointer, so there should be no performance difference between the 
> two. Without more details, it's hard to say what could be the reason 
> behind the bad performance.
> 
> L.

This issue comes up frequently enough that I'm starting to think "check 
against null literal" should maybe be special cased.  It even bites me 
from time to time and I know what I'm supposed to do.

--bb



More information about the Digitalmars-d mailing list