On 02/03/2011 02:27 PM, Steven Schveighoffer wrote:
On Wed, 02 Feb 2011 11:35:50 -0500, spir <denis.s...@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."

Thank you very much Steven to take the time to explain this, and very clearly. Actually, I had understood this, but was mislead by another fact interacting with the issue discussed here: D interns string literals, so that 2 string struct members that happen to be literals /look like/ beeing compared by value:

struct S {string s;}
unittest {
    // literals
    string s01 = "hello"; string s02 = "hello";
    assert ( S(s01) == S(s02) );
    assert (s01 is s02);        // additional info
    // concat
    string s1 = "he"; string s2 = "llo";
    string s3 = "hel"; string s4 = "lo";
    assert ( S(s1 ~ s2) != S(s3 ~ s4) );
    // slice
    string s = "hello";
    assert ( S(s[1..$-1]) != S("ell") );
    // idup'ed
    assert ( S(s[1..$-1].idup) != S("ell") );
    auto s5 = s[1..$-1].idup;
    assert ( S(s5) != S("ell") );
}

The case of literals passes as expected. Actually, s01 & s02 are the same piece of data in memory:
    assert (s01 is s02);
but if one doesn't know dmd interns string literals, all looks like behaving as if they were compared by value. (Hope I'm clear.)

Side-questions: is it written somewhere dmd interns string literals? If yes, where? Is this supposed to be part of D's spec or an implementation aspect of dmd?

Denis
--
_________________
vita es estrany
spir.wikidot.com

Reply via email to