Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #58564

Re: Lexical Variables In C Loops (As Compared To Perl Loops)

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: Lexical Variables In C Loops (As Compared To Perl Loops)
Date 2015-03-21 08:43 +0000
Organization Freie Universitaet Berlin
Message-ID <cn4p8qF20elU1@mid.uni-berlin.de> (permalink)
References <ZK2dnYy7U9u2vpDInZ2dnUVZ57ydnZ2d@giganews.com>

Show all headers | View raw


Robbie Hatley <see.my.sig@for.my.address> wrote:

> I was wondering if a variable declared inside a for loop in
> a C program is defined (memory address allocated) just once,
> or if (as in Perl) a fresh (unique) memory address is used
> for each iteration. So I wrote this program to find out:

> /* lexical-var-test.c */
> #include <stdio.h>
> int * array[12];
> int main(int argc, char *argv[])
> {
>     int i = 0;
>     for ( i = 1 ; i <= 10 ; ++i ) {
>        int var = i;
>        array[i] = &var;
>        printf("Addr = %p  Value = %d\n", (void*)array[i], *array[i]);
>     }
>     return 0;
> }

This program doesn't tell you much about your question because
you left the most probable thing out of consideration: the loop
variable isn't anywhere in memory but just in a CPU register (or
in one of the CPU caches or perhaps even optimized out completely
if the compiler finds some way to do that). But by taking the
address of the variable you force the compiler to use a memory
location for it. And, not urprisingly, a not too stupid compiler
figures that it can use the same location for all rounds of the
loop.

> So while the value changes, the address stays the same, meaning that
> the variable "var" is declared & defined & allocated ONLY ON FIRST
> ITERATION; after that, only the value changes.

That there's some "allocation" is, in all probability, an arte-
fact of your taking the address of the loop variable.

> ***** QUESTION: *****
> Is this behavior mandated by the C standard? If so, where? I've been
> looking, but can't find where it discusses this.

Not that I know of. Why should the standard prescribe how the
compiler does its work? All the standard cares about is the
scope of the loop variable. If the compiled code satisifies
that requirement it's fine, whatever method it uses. You'll
have to look at the assembler generated to see what really
happens under the hood (and that's, of course, specific to
the compiler you use).

> For comparison, Perl handles variables VERY differently. Scope, duration,
> definitions, etc.  Check out THIS program:

> use v5.014.004;
> use strict;
> use warnings;
> our @array;
> for (1..10) {
>     my $var = $_;
>     $array[$_] = \$var;
> }
> for (1..10) {
>     say("Addr = $array[$_]  Value = ${$array[$_]}");
> }

Also this, IMHO, doesn't test what you set out to prove. Again,
by taking the address of $var, storing it in an array and then
using that outside the loop you have enforced that there's some-
thing in memory and that it is kept past the end of the first
loop. If you had just used the value of $_ in the loops the
Perl interpreter could have done something different. And,
what you get via the '\' operator is not an address but a
reference, and Perl does reference counting since it has a
GC, so things are rather different from C.

> Which shows that in Perl, a variable declared in a for loop
> is re-declared & re-defined & re-allocated ON EVERY SINGLE
> ITERATION.  Very, very different from C.

I'm not convinced. Moreover, the nearer equivalent of the C
program (to avoid creating additional references in the loop
bock by storing in @array, wich C doesn't do) would be

use strict;
use warnings;
print "Addr = ", \$_, "  Value = $_\n" for 1..10;

And with that you will find that $_ always has the same address.
So no principle difference at all to the behaviour of the C pro-
gram... But for the details of Perl I guess comp.lang.perl.misc
will be the better place for further discussions.

                            Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Lexical Variables In C Loops (As Compared To Perl Loops) Robbie Hatley <see.my.sig@for.my.address> - 2015-03-21 00:52 -0700
  Re: Lexical Variables In C Loops (As Compared To Perl Loops) Richard Heathfield <rjh@cpax.org.uk> - 2015-03-21 08:28 +0000
    Re: Lexical Variables In C Loops (As Compared To Perl Loops) Robbie Hatley <see.my.sig@for.my.address> - 2015-03-26 20:57 -0700
  Re: Lexical Variables In C Loops (As Compared To Perl Loops) Keith Thompson <kst-u@mib.org> - 2015-03-21 01:29 -0700
    Re: Lexical Variables In C Loops (As Compared To Perl Loops) Robbie Hatley <see.my.sig@for.my.address> - 2015-03-26 21:01 -0700
  Lexical Variables In C Loops (As Compared To Perl Loops) asetofsymbols@gmail.com - 2015-03-21 01:40 -0700
  Re: Lexical Variables In C Loops (As Compared To Perl Loops) jt@toerring.de (Jens Thoms Toerring) - 2015-03-21 08:43 +0000
    Re: Lexical Variables In C Loops (As Compared To Perl Loops) Robbie Hatley <see.my.sig@for.my.address> - 2015-03-26 20:58 -0700
      Re: Lexical Variables In C Loops (As Compared To Perl Loops) Richard Heathfield <rjh@cpax.org.uk> - 2015-03-27 06:12 +0000
      Re: Lexical Variables In C Loops (As Compared To Perl Loops) Keith Thompson <kst-u@mib.org> - 2015-03-26 23:32 -0700
  Re: Lexical Variables In C Loops (As Compared To Perl Loops) Bartc <bc@freeuk.com> - 2015-03-21 10:54 +0000
    Re: Lexical Variables In C Loops (As Compared To Perl Loops) Keith Thompson <kst-u@mib.org> - 2015-03-21 13:54 -0700
    Re: Lexical Variables In C Loops (As Compared To Perl Loops) Robbie Hatley <see.my.sig@for.my.address> - 2015-03-26 21:24 -0700
      Re: Lexical Variables In C Loops (As Compared To Perl Loops) glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-03-27 20:35 +0000
      Re: Lexical Variables In C Loops (As Compared To Perl Loops) Bartc <bc@freeuk.com> - 2015-03-27 21:24 +0000
        Re: Lexical Variables In C Loops (As Compared To Perl Loops) glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-03-28 00:03 +0000
          Re: Lexical Variables In C Loops (As Compared To Perl Loops) Bartc <bc@freeuk.com> - 2015-03-28 10:00 +0000
        Re: Lexical Variables In C Loops (As Compared To Perl Loops) Stephen Sprunk <stephen@sprunk.org> - 2015-03-28 18:14 -0500
  Re: Lexical Variables In C Loops (As Compared To Perl Loops) Kaz Kylheku <kaz@kylheku.com> - 2015-03-21 14:39 +0000
  Re: Lexical Variables In C Loops (As Compared To Perl Loops) fir <profesor.fir@gmail.com> - 2015-03-21 09:40 -0700
  Re: Lexical Variables In C Loops (As Compared To Perl Loops) James Kuyper <jameskuyper@verizon.net> - 2015-03-21 12:43 -0400
    Re: Lexical Variables In C Loops (As Compared To Perl Loops) Philip Lantz <prl@canterey.us> - 2015-03-21 13:01 -0700
      Re: Lexical Variables In C Loops (As Compared To Perl Loops) James Kuyper <jameskuyper@verizon.net> - 2015-03-22 18:47 -0400
        Re: Lexical Variables In C Loops (As Compared To Perl Loops) Keith Thompson <kst-u@mib.org> - 2015-03-22 16:39 -0700
          Re: Lexical Variables In C Loops (As Compared To Perl Loops) James Kuyper <jameskuyper@verizon.net> - 2015-03-22 20:08 -0400
            Re: Lexical Variables In C Loops (As Compared To Perl Loops) Robbie Hatley <see.my.sig@for.my.address> - 2015-03-26 22:36 -0700
              Re: Lexical Variables In C Loops (As Compared To Perl Loops) Keith Thompson <kst-u@mib.org> - 2015-03-26 23:40 -0700
        Re: Lexical Variables In C Loops (As Compared To Perl Loops) Robbie Hatley <see.my.sig@for.my.address> - 2015-03-26 22:26 -0700
          Re: Lexical Variables In C Loops (As Compared To Perl Loops) Keith Thompson <kst-u@mib.org> - 2015-03-26 23:38 -0700

csiph-web