Pointers vs. References

Michael Neumann mneumann at ntecs.de
Wed Jun 13 04:56:43 PDT 2007


Dan wrote:
> 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.
> 
> Heh... using classes in any language will cut your performance compared to using structs.  D has nice structs.  References are pointers to pointers to something; not "the same".  Also, using while(x !is null) after explicitly defining it is usually wrong; you usually want do { ... } while(x !is null);  That's all I can gather though from your vague post.

Thanks for all your answers.

Well, the C++ version of course uses classes as well. It's a literal 
translation from C++ to D.
For those interested, the source is available via my mercurial 
repository at: http://ntecs.de/hg-projects/inspire/ (branch "d").

   hg clone static-http://ntecs.de/hg-projects/inspire/
   cd inspire
   hg update -C d

Regards,

   Michael



More information about the Digitalmars-d mailing list