stack variables and loops

Wesley Smith wesley.hoke@gmail.com
Wed Mar 19 05:36:00 GMT 2008


Hi,
I'm sure this is a simple question for all of the hardcore compiler
people on this list, but I'm about how compilers (and specifically
GCC) handle stack variables allocated exterior to versus interior to a
for loop.  For example:


//assume Vec3 is a triplet of floats and Quat is a quaternion (quadruplet)

/////example 1

Vec3 *v = vectors;
for(int i=0; i < 100; i++) {
       Vec3 v_rand;
        v_rand.random();   //make a random vector;

          *v++ += v_rand;
}


/////example 2

Vec3 *v = vectors;
 Vec3 v_rand;
for(int i=0; i < 100; i++) {
        v_rand.random();   //make a random vector;

          *v++ += v_rand;
}


Clearly there are scope differences between the 2 examples, but in
terms of performance, how do compilers handle these situations.
Obviously for these examples to be "equivalent", v_rand would have to
only be referenced inside the loop and nowhere else in the latter
example.

Apologies if this if this post is inappropriate or too speculative.
I'm just trying to get a sense of how the underlying compiler
technology works in these situations.

best,
wes



More information about the Gcc-help mailing list