public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Declaring automatic variables inside a while loop
@ 2004-01-20 23:42 Swaminathan Venkatakrishnaprasad
  2004-01-21  7:44 ` Jim Wilson
  0 siblings, 1 reply; 4+ messages in thread
From: Swaminathan Venkatakrishnaprasad @ 2004-01-20 23:42 UTC (permalink / raw)
  To: gcc

Hello,

Could you please let me know if it is 
valid to declare an automatic variable 
inside a for loop. If yes, is it also allowed
in ANSI standard (could you please refer
me to the sec #).

Example:
void f()
{
    int i;
    i = 10;
    while(i >=0 )
    {
       int j;
       i=j;
       printf("j=%d \n", j);
       i--;
    }
return;
}

Also, in the above example would
the number of iterations contribute anything to the stack
size since an automatic variable is declared
inside the while loop??

Your input is very much appreciated.

Thanks & Regards,
Swaminathan Venkat

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

* Re: Declaring automatic variables inside a while loop
  2004-01-20 23:42 Declaring automatic variables inside a while loop Swaminathan Venkatakrishnaprasad
@ 2004-01-21  7:44 ` Jim Wilson
  2004-01-21 22:30   ` Andrea 'Fyre Wyzard' Bocci
  0 siblings, 1 reply; 4+ messages in thread
From: Jim Wilson @ 2004-01-21  7:44 UTC (permalink / raw)
  To: Swaminathan Venkatakrishnaprasad; +Cc: gcc

Swaminathan Venkatakrishnaprasad wrote:
> Could you please let me know if it is 
> valid to declare an automatic variable 
> inside a for loop.

This has always been legal.  The loop body is just a compound statement, 
  same as a function body.

> If yes, is it also allowed
> in ANSI standard (could you please refer
> me to the sec #).

3.6.2, Compound Statement or Block, in the 1989 ANSI C standard.

> Also, in the above example would
> the number of iterations contribute anything to the stack
> size since an automatic variable is declared
> inside the while loop??

No.  The variable is allocated only once.

By the way, in your example, you are setting the loop iterator (i) to an 
uninitialized local (j) which isn't going to work.
-- 
Jim Wilson, GNU Tools Support, http://www.SpecifixInc.com

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

* Re: Declaring automatic variables inside a while loop
  2004-01-21  7:44 ` Jim Wilson
@ 2004-01-21 22:30   ` Andrea 'Fyre Wyzard' Bocci
  2004-01-22  0:21     ` Robert Dewar
  0 siblings, 1 reply; 4+ messages in thread
From: Andrea 'Fyre Wyzard' Bocci @ 2004-01-21 22:30 UTC (permalink / raw)
  To: Jim Wilson; +Cc: Swaminathan Venkatakrishnaprasad, gcc


>By the way, in your example, you are setting the loop iterator (i) to an 
>uninitialized local (j) which isn't going to work.

Well, it's going to "work", if you don't care what the actual behaviour is 
- ie., if the code hangs at random executions...

.fw. 


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

* Re: Declaring automatic variables inside a while loop
  2004-01-21 22:30   ` Andrea 'Fyre Wyzard' Bocci
@ 2004-01-22  0:21     ` Robert Dewar
  0 siblings, 0 replies; 4+ messages in thread
From: Robert Dewar @ 2004-01-22  0:21 UTC (permalink / raw)
  To: Andrea 'Fyre Wyzard' Bocci
  Cc: Jim Wilson, Swaminathan Venkatakrishnaprasad, gcc

Andrea 'Fyre Wyzard' Bocci wrote:

> 
>> By the way, in your example, you are setting the loop iterator (i) to 
>> an uninitialized local (j) which isn't going to work.
>  
> Well, it's going to "work", if you don't care what the actual behaviour 
> is - ie., if the code hangs at random executions...
        ^^

I assume you meant eg here (i.e. this is an example), since this is only
one of many horrible things that could happen :-)



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

end of thread, other threads:[~2004-01-22  0:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-20 23:42 Declaring automatic variables inside a while loop Swaminathan Venkatakrishnaprasad
2004-01-21  7:44 ` Jim Wilson
2004-01-21 22:30   ` Andrea 'Fyre Wyzard' Bocci
2004-01-22  0:21     ` Robert Dewar

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).