default '==' on structs

Steven Schveighoffer schveiguy at yahoo.com
Thu Feb 3 05:27:12 PST 2011


On Wed, 02 Feb 2011 11:35:50 -0500, spir <denis.spir at gmail.com> wrote:

> On 02/02/2011 04:20 PM, Lars T. Kyllingstad wrote:

>> I think the compiler does a bitwise comparison in this case, meaning  
>> that
>> it compares the arrays' pointers instead of their data.  Related bug
>> report:
>
> Thank you, Lars.
> In fact, I do not really understand what you mean. But it helped me  
> think further :-)

I couldn't get from all your posts that you understand the issue.  A  
bitwise comparison compares ONLY the bits in the struct, NOT what the  
struct points to.

Comparing two arrays compares the data they point to.  So what is  
happening is essentially, the struct default comparison is comparing that  
both strings are equal in the identity sense, i.e. they both point to the  
exact same data with the exact same length.

If you analyze a string array, it looks like this (switch to mono-spaced  
font now :) :


+----------------------+
|int length            |
|immutable(char) *ptr -|------> "hello world"
+----------------------+

The pointer points to the data, it is not contained within the array  
"head".  The bitwise comparison only compares the head (what's in the box).

Apologies if you already understood this, but I wanted to be sure that you  
"got it."

-Steve


More information about the Digitalmars-d-learn mailing list