www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12360] New: struct field not actually updated

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12360

           Summary: struct field not actually updated
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: lionel.thiry gmail.com



PDT ---
I was testing dmd 2.065 with some old work of mine and came upon this bug. I've
tried my best to reduce it to this (first time ever I post a bug report, sorry
if I don't do it the right way):

struct BufferedArray(/*of*/T) {
    T[] m_array;
    uint index=0;
    this( T[] array ) { m_array = array; }
}

void push(T)( T Element, /*on*/BufferedArray!(T) array ) {
    array.m_array[array.index] = Element;
    array.index++;
}

unittest {
    auto test_array = BufferedArray!(/*of*/string)( new string[10] );

    push( "Hello", test_array );
    push( "world", test_array );
    push( "!", test_array );

    assert( test_array.index == 3 ); //fails
}

As the bug was particulary unclear to me, I inserted a bunch of writeln's which
clearly show that index is updated inside the push function but as soon as push
exits, index is zero again. The field is not actually updated.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 13 2014
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12360


Adam D. Ruppe <destructionator gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |destructionator gmail.com



20:03:05 PDT ---
You should take the buffered array as ref in the push function. Otherwise, it
is passed as value, so it updates in the function, but those changes aren't
preserved outside the function.

So it isn't really a bug, though it might have worked a few years ago because
D1 array slices were a weird value/reference hybrid. (Well, they still are a
hybrid, but they aren't so weird anymore and consistently act like a value when
appending; see: http://dlang.org/d-array-article.html )

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 13 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12360


Lionel Thiry <lionel.thiry gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID



PDT ---
Thanks for your fast answer. struct are passed by value, how did I forget that?
So many years without programming, probably... I suppose I should close this
issue. Sorry for the noise.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 13 2014