From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17892 invoked by alias); 19 Mar 2008 05:36:57 -0000 Received: (qmail 17884 invoked by uid 22791); 19 Mar 2008 05:36:56 -0000 X-Spam-Check-By: sourceware.org Received: from wa-out-1112.google.com (HELO wa-out-1112.google.com) (209.85.146.183) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 19 Mar 2008 05:36:37 +0000 Received: by wa-out-1112.google.com with SMTP id m16so225561waf.20 for ; Tue, 18 Mar 2008 22:36:35 -0700 (PDT) Received: by 10.114.110.12 with SMTP id i12mr568267wac.73.1205904995503; Tue, 18 Mar 2008 22:36:35 -0700 (PDT) Received: by 10.114.36.17 with HTTP; Tue, 18 Mar 2008 22:36:35 -0700 (PDT) Message-ID: <1079b050803182236ud33b5bfqe4148ff53dcc2681@mail.gmail.com> Date: Wed, 19 Mar 2008 05:36:00 -0000 From: "Wesley Smith" To: "MSX to GCC" Subject: stack variables and loops MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2008-03/txt/msg00173.txt.bz2 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