I have a scene class with member variables Image **images, int * xcoords, int * ycoords. Now, I’m trying to overload my = operator.
I get the following memory errors (using valgrind)
Conditional jump or move depends on uninitialised value(s)
==6439== at 0x406FCA: Scene::drawscene() const (scene.cpp:160)
==6439== by 0x4084C1: main (testscene.cpp:50)
And the line in question from above (scene.cpp:160) is
if (images[i]!=NULL)
So theyre saying that images was not initialized.
And so anywhere else drawscene() was called did not cause any problems, but I think because the = operator was used, it caused a problem.
Can anyone see any problems in my code that could cause this error?
You need this:
and that will solve your immediate problem. But really, you’re going about this the wrong way. As @Gman said, you should be using the copy-and-swap idiom.
The reason to use the idiom is exception safety. If something throws an exception in the middle of that
operator =(and there is plenty that can since it’s doing so much) you will be leaving the object in an undefined state, and that’s very bad. The copy and swap idiom lets you write an exception safe copy-constructor (which is still a little tricky) and then leverage it to build an assignment operator.As an added bonus, you get a working copy constructor and a
swapfunction. The copy constructor is very handy for stuffing things in STL containers and passing or returning things by value. And theswapfunction is is pretty darned useful for users of your class, especially ones who would like to implement their own exception safe copy constructors and assignment operators.