#### Archived

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

# Passing by reference. Why didn't this work?

This topic is 4983 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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 on other sites
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]