To Point or Not To Point?
Can someone tell me how you decide wether to use pointers to your functions or not, for example, take a function that gets the length of a vector.
is it better to use
VectorLength(vector3 *vector);
or
VectorLength(vector3 vector);
Does it make a difference if the function returns a value?
If someone can go over on when deciding to pass pointers to functions, let me know, thanks
In the case of VectorLength (vector3 v), the whole structure will be placed onto the stack prior to the routine being called. In the case of VectorLength (vector3 *v), only the pointer is being passed.
Thus the pointer is faster when passing strutures of size.
Tim
Thus the pointer is faster when passing strutures of size.
Tim
Size is one issue. Also speed of allocation and whether the called function is modifying the parameter for the called function. If you just allocate the structure to pass it to the function and make no other use of it then it is faster on the stack, i.e. VectorLength(vector3()). In practice you generally can''t count on that since the function is used in many places so you use the size instead. If the function modified the parameter such as VectorRotate(vector3 &vector) rotating the vector instead of returning a new vector then you have to have a pointer to the data item the calling function is using. Otherwise the changes are just thrown away when the function exits.
Keys to success: Ability, ambition and opportunity.
I only use (explicit) pointers when I want to twiddle the parameter being passed in.
It''s generally a good idea to use pointers to large amounts data (anything larger than ~32bytes) because it is likely faster to do so.
However, in that case I don''t use a pointer (becuase a pointer means that I want to change it). I use a constant reference (which really is a pointer, but it tells everyone that you''re not changing the value of the thing pointed at).
So in the example that you gave, I''d do a...
It''s generally a good idea to use pointers to large amounts data (anything larger than ~32bytes) because it is likely faster to do so.
However, in that case I don''t use a pointer (becuase a pointer means that I want to change it). I use a constant reference (which really is a pointer, but it tells everyone that you''re not changing the value of the thing pointed at).
So in the example that you gave, I''d do a...
float VectorLength(const vector3& vector){float x,y,z,r;x=vector.x;x*=x;y=vector.y;y*=y;z=vectot.z;z*=z;r=x+y+z;r = fsqrt(r);return(r);}
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement