public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* stack variables and loops
@ 2008-03-19  5:36 Wesley Smith
  2008-03-19 13:18 ` Diego Novillo
  0 siblings, 1 reply; 3+ messages in thread
From: Wesley Smith @ 2008-03-19  5:36 UTC (permalink / raw)
  To: MSX to GCC

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-03-19 15:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-19  5:36 stack variables and loops Wesley Smith
2008-03-19 13:18 ` Diego Novillo
2008-03-19 15:24   ` Wesley Smith

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).