Return a dynamic array: bug or feature?
Gilles G.
schaouette at free.fr
Tue Jul 3 23:28:07 PDT 2007
Hello,
I went into trouble returning a dynamic array from a function. For example:
import std.stdio;
int[] testGood()
{
int a[]; a.length=2;
a[]=1;
return a;
}
int[] testBad()
{
int a[2];
a[]=1;
return a;
}
int main()
{
int b[];
b = testGood();
writef("Good test:\n");
writef(" b[0]=%d, b[1]=%d\n",b[0],b[1]);
writef(" b[0]=%d, b[1]=%d\n",b[0],b[1]);
b = testBad();
writef("Bad test:\n");
writef(" b[0]=%d, b[1]=%d\n",b[0],b[1]);
writef(" b[0]=%d, b[1]=%d\n",b[0],b[1]);
return 0;
}
This code outputs:
>Good test:
> b[0]=1, b[1]=1
> b[0]=1, b[1]=1
>Bad test:
> b[0]=0, b[1]=1
> b[0]=0, b[1]=1
I guess this is the good behavior since I should not return a static array from a function, but the compiler _should_ issue a warning here... or not?
--
Gilles
More information about the Digitalmars-d-learn
mailing list