[Issue 1148] New: Problems returning structs from functions
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Apr 15 08:31:11 PDT 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1148
Summary: Problems returning structs from functions
Product: D
Version: 1.010
Platform: PC
OS/Version: Linux
Status: NEW
Severity: major
Priority: P2
Component: DMD
AssignedTo: bugzilla at digitalmars.com
ReportedBy: mtwatkin at mtu.edu
This issue may be related to issue 997, but it appears to be somewhat different
to me.
The code at the bottom of this error report is expected to print:
Returning <1.000000, 0.000000, 0.000000>
Returned: <1.000000, 0.000000, 0.000000>
However, the actual result is
Returning <1.000000, 0.000000, 0.000000>
Returned: <0.000000, 0.000000, 0.000000>
if "Vector b = Vector(); return b;" is changed to "return Vector();" in foobar,
the code appears to work properly. Depending on the parameters passed to the
function, the result changes. For example, if bar is declared "private void
bar(uint x, uint y)", and is called from foo with "bar(2, 3)", then it will
print:
Returning <1.000000, 0.000000, 0.000000>
Returned: <0.000000, 1.000000, 0.000000>
If the contents of bar are moved into foo, and the call to bar is removed, the
code appears to work properly. if the contents of bar are changed to
Vector c = foobar();
Vector();
writef("Returned: %s\n", c);
then the printed result is nondeterministic. Here is a sample output:
Returning <1.000000, 0.000000, 0.000000>
Returned: <-1.697432, 0.000000, 0.000000>
I am compiling the following code with "dmd -v -debug -g -op -L-lphobos
src/main.d" on Ubuntu Linux 6.10
contents of src/main.d
--------------------------------------------------------------------
import std.stdio;
import std.string;
struct Vector
{
float x, y, z;
public static Vector opCall(float x = 0, float y = 0, float z = 0)
{
Vector v;
v.x = x;
v.y = y;
v.z = z;
return v;
}
public char[] toString()
{
return format("<%f, %f, %f>", x, y, z);
}
}
class Foo
{
private Vector v;
public this()
{
v = Vector(1, 0, 0);
}
public void foo()
{
bar();
}
private void bar()
{
writef("Returned: %s\n", foobar());
}
public Vector foobar()
{
writef("Returning %s\n", v);
return v;
Vector b = Vector();
return b;
}
}
void main(char [][] args)
{
Foo f = new Foo();
f.foo();
}
--
More information about the Digitalmars-d-bugs
mailing list