Thread overview
[Issue 9651] New: Returning a newly-created slice by reference
Mar 05, 2013
Ivan Kazmenko
Mar 05, 2013
Ivan Kazmenko
Mar 08, 2013
Andrej Mitrovic
March 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9651

           Summary: Returning a newly-created slice by reference
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: gassa@mail.ru


--- Comment #0 from Ivan Kazmenko <gassa@mail.ru> 2013-03-05 12:00:51 PST ---
There is a handy warning in DMD of the sort "escaping reference to a local variable".  Below is a similar case which is wrong code but does not generate a similar warning.

We declare a function returning a reference to a slice.  Inside, we return a reference to a newly-created slice of some array.  This is an error but goes undetected by the compiler.

Example 1 (fixed length array):
-----
import std.stdio;

int [1] a;

ref int [] f ()
{
    return a [0..1];
}

void main ()
{
    auto s = f ();
    writeln (s);
}
-----

Example 2 (variable length array):
-----
import std.stdio;

int [] a;

ref int [] f ()
{
    return a [0..1];
}

void main ()
{
    a = new int [1];
    auto s = f ();
    writeln (s);
    auto t = f ();
    writeln (t);
}
-----

With DMD 2.062, the first example locally crashes with Access Violation after printing ~10 Mb of some garbage array slice.  The second example prints an empty slice (instead of [0]) for s and then crashes for t.

Now, while returning "ref int []" instead of "int []" might have its uses, in my case it was a newbie mistake.  I just wanted to return a slice of lvalues and didn't understand that the elements of the "int []" will be assignable for free.  Thought I need an extra "ref" for that.

The case seems obvious: the function returns by reference an entity which was created on the stack just a few instructions before that.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9651


Ivan Kazmenko <gassa@mail.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |http://d.puremagic.com/issu
                   |                            |es/show_bug.cgi?id=4451


--- Comment #1 from Ivan Kazmenko <gassa@mail.ru> 2013-03-05 12:05:11 PST ---
Interestingly, a local array and a delegate do generate a warning.  However, the warning says "escaping reference to local variable a" which is not true: the problem lies in escaping reference to a local unnamed slice, not variable "a" which is legally visible to the caller.  The non-ref versions compile and run without problems.

Example 3 (local fixed length array):
-----
import std.stdio;

void main ()
{
    int [1] a;

    ref int [] f ()
    {
        return a [0..1];
    }

    auto s = f ();
    writeln (s);
}
-----

Example 4 (local variable length array):
-----
import std.stdio;

void main ()
{
    int [] a;

    ref int [] f ()
    {
        return a [0..1];
    }

    a = new int [1];
    auto s = f ();
    writeln (s);
    auto t = f ();
    writeln (t);
}
-----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9651


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |andrej.mitrovich@gmail.com
         Resolution|                            |DUPLICATE


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-03-07 18:50:47 PST ---
Issue 2486 fixed this.

*** This issue has been marked as a duplicate of issue 2486 ***

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