www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - local_array ~ null_array leading to AV

reply Vathix <vathix dprogramming.com> writes:
int main()
{
    Object[] array = null;

    void dofirst()
    {
       Object o1 = new Exception("first");
       //array = (&o1)[0 .. 1] ~ array; // Causes Access Violation when  
using toString() on the array...
       array = (&o1)[0 .. 1].dup ~ array; // Works.
    }

    dofirst();
    Object o2 = new Exception("second");
    array ~= o2;

    printf("array[0] = %.*s; array[1] = %.*s;", array[0].toString(),  
array[1].toString()); // ...here

    return 0;
}


Shouldn't ~ create a safe new heap array even if its operands are local? I  
had to put that first one in the nested function so it could screw with  
the stack I guess. This is a dumbed down example of something much larger  
in my code that heavily (in many areas) relies on ~ copying.
May 20 2005
parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"Vathix" <vathix dprogramming.com> wrote in message 
news:op.sq22b3takcck4r esi...
 int main()
 {
    Object[] array = null;

    void dofirst()
    {
       Object o1 = new Exception("first");
       //array = (&o1)[0 .. 1] ~ array; // Causes Access Violation when 
 using toString() on the array...
       array = (&o1)[0 .. 1].dup ~ array; // Works.
    }

    dofirst();
    Object o2 = new Exception("second");
    array ~= o2;

    printf("array[0] = %.*s; array[1] = %.*s;", array[0].toString(), 
 array[1].toString()); // ...here

    return 0;
 }


 Shouldn't ~ create a safe new heap array even if its operands are local? I 
 had to put that first one in the nested function so it could screw with 
 the stack I guess. This is a dumbed down example of something much larger 
 in my code that heavily (in many areas) relies on ~ copying.
It is true the documentation says ~ always dups even if one of the inputs is empty. It looks like in practice it does not dup if one of the inputs is empty. Either the doc is out of date or ~ needs a fix.
May 20 2005