• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Compiled code doesn't do what I expect
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Compiled code doesn't do what I expect


  • Subject: Re: Compiled code doesn't do what I expect
  • From: Tommy Nordgren <email@hidden>
  • Date: Sat, 25 Feb 2006 17:03:16 +0100


On 25 feb 2006, at 16.48, Jonathan Taylor wrote:

Can anyone shed any light on why this code doesn't run as expected?

struct vector3D
{
	double	x, y, z;

vector3D() { }
vector3D(double inX, double inY, double inZ) { x = inX; y = inY; z=inZ; }
vector3D& operator += (const vector3D &n) { x += n.x; y += n.y; z += n.z; return *this; }
vector3D& operator + (const vector3D &n) const { return vector3D (*this) += n; }
vector3D& operator *= (double n) { x *= n; y *= n; z *= n; return *this; }
vector3D& operator * (double n) const { return vector3D(*this) *= n; }
vector3D operator = (const vector3D &n) { x = n.x; y = n.y; z = n.z; return *this; }
void Print(void) const { printf("(%lg, %lg, %lg)", x, y, z); }
};


void do_test(void)
{
	vector3D	rayPos(0, 1, 1), rayDir(1, 0, 0);
	double		propagationDistance = 10.5;
	vector3D	newRayPos;

	newRayPos = rayPos + rayDir * propagationDistance;
	newRayPos.Print();
	vector3D temp = rayDir * propagationDistance;
	newRayPos = rayPos + temp;
	newRayPos.Print();
}

When I run it (with all optimizations off) I see the following output:

	(0, 2, 2)
	(10.5, 1, 1)

I would expect both lines to be identical.

The problem seems to be connected to the use of the "vector3D&" return type for the various operators. If I remove the "&" the program works as expected. Note that if I add an "&" to the return type for "operator=" then neither line of output contains the correct result.

Am I doing something illegal here or is the compiler generating bad code? I'd like to be able to understand this because the code runs much slower if I pass vector3D structures around on the stack...

Yes. You are returning a reference to a anonymous vector3D, created on the stack. This memory will be recycled when your
operator functions returns.
You should return a vector3D, and not a vector3D reference from operator * and operator +
See for example Stroustrup: The C++ programming language


Thanks
Jonny
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40chello.se


This email sent to email@hidden

------------------------------------- This sig is dedicated to the advancement of Nuclear Power Tommy Nordgren email@hidden



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Compiled code doesn't do what I expect (From: Jonathan Taylor <email@hidden>)

  • Prev by Date: Compiled code doesn't do what I expect
  • Next by Date: Re: [ANN] Step into Xcode
  • Previous by thread: Compiled code doesn't do what I expect
  • Next by thread: FileMerge Replacements
  • Index(es):
    • Date
    • Thread