vector1 = vector2 * 2.3 = debug assertion failed?

Started by
17 comments, last by Kyo 21 years, 8 months ago
I tried it doesn''t let me though.
Advertisement
I could not find any problems with your code, other than the array issue as stated above.

I created a CVector class based on your code and had no problems with it. I would like to see how you implement the CVector class in your code.
Here''s the code in full.

CVector.h


  #ifndef CVECTOR_H#define CVECTOR_H#include <math.h>#include <windows.h>class CVector{public:	//constructors	CVector();	CVector(float a, float b, float c, float d); 	~CVector();	//accessor functions 	/*	const float& operator[] (int i) const { return v; }	//to read<br>	float& operator() (int i) { return v; }				//to write<br>	const float* ReadArray(void) { return v; }				<br>	float* GetArray(void) { return v; }*/</font><br><br><font color="gray">//private:<br></font><br>	<font color="blue">float</font> v[<font color="purple">4</font>];<br><br>};<br><br><br><font color="gray">//vector function declarations<br></font><br><br><font color="blue">extern</font> <font color="blue">inline</font> CVector operator* (const CVector& vec, <font color="blue">float</font> s);<br><font color="blue">extern</font> <font color="blue">inline</font> CVector operator* (<font color="blue">float</font> s, const CVector& vec);<br><font color="blue">extern</font> <font color="blue">inline</font> CVector& operator*= (CVector& vec, <font color="blue">float</font> s);<br><font color="blue">extern</font> <font color="blue">inline</font> CVector operator- (const CVector& vec);<br><font color="blue">extern</font> <font color="blue">inline</font> CVector operator/ (const CVector& vec, <font color="blue">float</font> s);<br><font color="blue">extern</font> <font color="blue">inline</font> CVector& operator/= (CVector& vec, <font color="blue">float</font> s);<br><br><font color="blue">extern</font> <font color="blue">inline</font> CVector operator+ (const CVector& v1, const CVector& v2);<br><font color="blue">extern</font> <font color="blue">inline</font> CVector& operator+ (CVector& v1, const CVector& v2);<br><font color="blue">extern</font> <font color="blue">inline</font> CVector operator- (const CVector& v1, const CVector& v2);<br><font color="blue">extern</font> <font color="blue">inline</font> CVector& operator- (CVector& v1, const CVector& v2); <br><font color="blue">extern</font> <font color="blue">inline</font> <font color="blue">bool</font> operator== (const CVector& v1, const CVector& v2);<br><br><font color="blue">extern</font> <font color="blue">inline</font> <font color="blue">float</font> dotProduct(const CVector& v1, const CVector& v2);<br><font color="blue">extern</font> <font color="blue">inline</font> CVector crossProduct(const CVector& v1, const CVector& v2);<br><br><font color="blue">extern</font> <font color="blue">inline</font> <font color="blue">float</font> lengthSquared(const CVector& vec);<br><font color="blue">extern</font> <font color="blue">inline</font> <font color="blue">float</font> length(const CVector& vec);<br><br><font color="blue">extern</font> <font color="blue">inline</font> CVector normalize(const CVector& vec);<br><br><br><br>#end<font color="blue">if</font><br>  </pre></DIV><!–ENDSCRIPT–><br><br>CVector.cpp (only 3 operator overloads done)<br><br>    <!–STARTSCRIPT–><BR><DIV CLASS=source><pre>  <br><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray">//——————————————————–<br></font></font></font></font></font></font></font></font></font></font><br><font color="gray">//constructors and destructor<br></font><br><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray">//——————————————————–<br></font></font></font></font></font></font></font></font></font></font><br>CVector::CVector()<br>{<br>	memset(v,0,<font color="blue">sizeof</font>(<font color="blue">float</font>(3)));<br>}<br><br>CVector::CVector(<font color="blue">float</font> a,<font color="blue">float</font> b,<font color="blue">float</font> c,<font color="blue">float</font> d)<br>{<br>	v[<font color="purple">0</font>] = a;<br>	v[<font color="purple">1</font>] = b;<br>	v[<font color="purple">2</font>] = c;<br>	v[<font color="purple">3</font>] = d;<br>}<br><br>CVector::~CVector()<br>{<br>	free(v);<br>}<br><br><br><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray">//——————————————————–<br></font></font></font></font></font></font></font></font></font></font><br><font color="gray">//vector times scalar<br></font><br><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray">//——————————————————–<br></font></font></font></font></font></font></font></font></font></font><br><font color="blue">inline</font> CVector operator* (const CVector& vec,<font color="blue">float</font> s)<br>{<br>	<font color="blue">return</font> CVector(s*vec.v[<font color="purple">0</font>],s*vec.v[<font color="purple">1</font>],s*vec.v[<font color="purple">2</font>],s*vec.v[<font color="purple">3</font>]);<br>}<br><br><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray">//——————————————————–<br></font></font></font></font></font></font></font></font></font></font><br><font color="gray">//scalar times vector<br></font><br><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray">//——————————————————–<br></font></font></font></font></font></font></font></font></font></font><br><font color="blue">inline</font> CVector operator* (<font color="blue">float</font> s,const CVector& vec)<br>{<br>	<font color="blue">return</font> vec * s;<br>}<br><br><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray">//——————————————————–<br></font></font></font></font></font></font></font></font></font></font><br><font color="gray">//vector times equal scalar<br></font><br><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray">//——————————————————–<br></font></font></font></font></font></font></font></font></font></font><br><font color="blue">inline</font> CVector& operator*= (CVector& vec,<font color="blue">float</font> s)<br>{<br>	<font color="blue">for</font> (<font color="blue">int</font> i=0;i&lt;4;i++)<br>	{<br>		vec.v[<font color="purple">i</font>] *= s;<br>	}<br>	<font color="blue">return</font> vec;<br>}<br><br><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray">//——————————————————–<br></font></font></font></font></font></font></font></font></font></font><br><font color="gray">//reverse vector<br></font><br><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray"><font color="gray">//——————————————————–<br></font></font></font></font></font></font></font></font></font></font><br><font color="blue">inline</font> CVector operator- (const CVector& vec)<br>{<br>	CVector tempvec(-vec.v[<font color="purple">0</font>],-vec.v[<font color="purple">1</font>],-vec.v[<font color="purple">2</font>],-vec.v[<font color="purple">3</font>]);<br>	<font color="blue">return</font> tempvec;<br>}<br><br>  </pre></DIV><!–ENDSCRIPT–><br><br>Part of mainapp.cpp (init is called from winmain, CVector and other classes declared globally)<br><br>    <!–STARTSCRIPT–><BR><DIV CLASS=source><pre>  <br>COpenGL ogl;		<font color="gray">//ogl object<br></font><br>CTexture texture;	<font color="gray">//texture object<br></font><br>CLight light;		<font color="gray">//lighting object<br></font><br>CVector vector2(0.4f,1.5f,-6.0f,1.0f);<br>CVector vector1(1.0f,1.0f,1.0f,1.0f);<br><br><font color="blue">bool</font> Init()<br>{<br>	<font color="gray">//vector2 * 1;  doesnt work<br></font><br>	<font color="gray">//-vector2;  doesnt work<br></font><br>	<font color="gray">//vector1 = vector2 * 1;  doesnt work<br></font><br>        vector1 = vector2 <font color="gray"><font color="gray">//works<br></font></font><br>        vector1 *= vector2 <font color="gray"><font color="gray">//works<br></font></font><br><br>	<font color="blue">if</font> (!(texture.LoadBMP(<font color="darkred">"Data\\treebark.bmp"</font>,1,true)))<br>	{<br>		<font color="blue">return</font> false;<br>	}<br><br>	light.EnableLight();<br>	light.SetAmbient(1, 0.9f, 0.9f, 0.9f, 1.0f);<br>	light.SetDiffuse(1, 1.0f, 1.0f, 1.0f, 1.0f);<br>	light.SetPosition(1, 0.0f, 0.0f, 2.0f, 1.0f);<br>	light.PointTo(1, -1.5f, 0.0f, -6.0f);<br>	light.SwitchOn(1);<br><br>	<font color="blue">return</font> true;<br>}<br><br>  </pre></DIV><!–ENDSCRIPT–><br><br>    
OK.
The reason for the debug assertion is because you are creating a temporary vector, and then returning it. When the temporary goes out of scope, the destructor is called. This calls free. I don''t see any calls to malloc in the constructor, so that is the bug.

Also, in tbe constructor you are calling memset with the third argument float( 3 ) .
That doesn''t do what you are expecting it to do. sizeof( float ( 3) ) == sizeof (float). I think you want sizeof(float) * 3.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Your error is being caused by the "free(v)" call in the CVector destructor.

Get rid of the memset call in the constructor and the free(v) call in the destructor and try it. It works fine for me.


To "Paradigm Shifter": returning a temorary object is completly valid, since the object will be copied, but returning a reference wouldn''t.
To "Paradigm Shifter": returning a temorary object is completly valid, since the object will be copied, but returning a reference wouldn''t.
And to Kyo: In VS, you can go to debug mode when the assertion dialog appers, then have a look at the call-stack. But I would agree with PigHeaded.
When doing an = operator overloading you also need to define a copy constructor like this:

CVector::CVector(const CVector &p){   // Copy constructor   if(this == &p)      return;   for(int i=0;i<3;i++)      v = p.v;<br>   <br>}<br><br> </pre> <br><br>  </i>   
I didn''t say that it was a problem returning a tmporary, just that the temporary object is still constructed and destructed in the usual manner... I expect the original poster didn''t realise that if you return CVector(...) then the nameless CVector is still constructed and destructed.



"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement