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


Groups > comp.lang.c > #58562

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

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Lexical Variables In C Loops (As Compared To Perl Loops)
Date 2015-03-21 01:29 -0700
Organization None to speak of
Message-ID <87h9tewy7g.fsf@kst-u.example.com> (permalink)
References <ZK2dnYy7U9u2vpDInZ2dnUVZ57ydnZ2d@giganews.com>

Show all headers | View raw


Robbie Hatley <see.my.sig@for.my.address> writes:
> 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;
> }
>
> And the results of running it are:
>
[snip]
>
> 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.
>
>
> ***** QUESTION: *****
> Is this behavior mandated by the C standard? If so, where? I've been
> looking, but can't find where it discusses this.

No, it's not mandated.  As far as the language is concerned, a
new instance of `var` is created on entry to the enclosing block,
and ceases to exist when execution reaches the closing "}".

But "allocating" and "deallocating" a local variable doesn't have
to involve any actual run-time action.  A compiler will likely
just allocation space for both `i` and `var` as part of the stack
frame on entry to main, and deallocate that space at once on exit.
It doesn't matter whether the various instances of `var` have the
same address or not, since logically no two of them exist at the
same time.

> 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[$_]}");
> }

One difference (among many) between C and Perl is that in Perl, you can
save a reference to $var and refer to it outside the scope in which it's
declared.  The existence of a reference means that it won't be
garbage-collected.  In C, you can take the address of a local variable
and refer to it outside the variable's scope -- but doing so results in
undefined behavior.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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