Archived

This topic is now archived and is closed to further replies.

okonomiyaki

Passing by reference. Why didn't this work?

Recommended Posts

okonomiyaki    548
Last night was one of those nights where I was banging my head against the wall for hours until I found the small unexpected bug in my program. I have an object class that calls a GetTexCoords() function in another class. Originally the function was defined as this:
void vertexStripper::GetTexCoords(float &u, float &v, vec2 vPos) {
  u = vPos.x/(dataSize-1);
  v = vPos.y/(dataSize-1);
}
However, tracing through my program shows that u or v sometimes does not get set correctly (looks like the garbage value you get when you declare a value without initializing). Half the time they did get set correctly. I changed it to this, and to my surprise, it worked:
float vertexStripper::GetTexCoordU(vec2 vPos) {
  return vPos.x/(dataSize-1);
}
float vertexStripper::GetTexCoordV(vec2 vPos) {
  return vPos.y/(dataSize-1);
}
Is there any reason why the first method should not have worked?? Maybe there is a bug elsewhere *shrugs*

Share this post


Link to post
Share on other sites
quorn    145
what is the copy constructor for vec2?
What type are the members x and y?
Are they initialised?
If they are not fundamental types have you defined operator/ for them?
is datasize == 1?
is datasize initialised?

these are all places that there could be bugs although I doubt them all. clutching at straws.

Share this post


Link to post
Share on other sites
okonomiyaki    548
Yeah, tracing through the program shows all of those places to be fine. x, y area floats, dataSize is an int that is always 257 (for now).
The main argument against all of that is that the second method works, which involves the same kind of operations. The only difference is that I'm not passing by reference, but explicitly saying vert[ i ].u = GetTexCoordU(vPos). Same for V value.
Who knows *shrugs* I've seen worse.

Thanks though!

edit: auto-formatted wrong

[edited by - okonomiyaki on May 27, 2004 11:19:21 AM]

Share this post


Link to post
Share on other sites
Matei    190
How did you call the original function? It looks like it should work fine. Try adding printf''s at the start and end of the function.

Share this post


Link to post
Share on other sites