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


Groups > comp.lang.c > #58600

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 13:54 -0700
Organization None to speak of
Message-ID <87d242vzoc.fsf@kst-u.example.com> (permalink)
References <ZK2dnYy7U9u2vpDInZ2dnUVZ57ydnZ2d@giganews.com> <mejiko$uvj$1@dont-email.me>

Show all headers | View raw


Bartc <bc@freeuk.com> writes:
> On 21/03/2015 07:52, Robbie Hatley 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:
>
>>     for ( i = 1 ; i <= 10 ; ++i ) {
>>        int var = i;
>
>
>> Addr = 0x23cb0c  Value = 1
>> Addr = 0x23cb0c  Value = 2
>
> That proves nothing. Maybe 'var' is reallocated each time, but it just
> happens that the stack pointer has the same value (and why not), and
> the address happens to be the same.
>
> And of course the compiler will take advantage of this. In fact,
> unless 'var' is implemented as pointer, 'var' *has* to have the same
> offset or address for each iteration.

No, it doesn't have to, though it's very likely to.

What's relevant to the lifetime of `var` is not the loop, but the block
within the loop, delimited by `{` and `}`.  The loop itself creates a
scope, and the block creates another scope within it; objects defined in
the block have a lifetime associated with the execution of the block.

Most compilers will allocate storage for the block identically each time
it's executed, resulting in the same address for each object defined in
the block.  (There's no 100% pedantically portable way to verify that,
since you can't compare addresses of objects that don't exist at the
same time while avoiding undefined behavior, but you can, for example,
print the address with printf("%p", (void*)&var).)  And in fact most
compilers will optimize the allocation by doing it just once on entry to
the function.

But imagine a hypothetical compiler that physically allocates storage
for the block each time it's entered, and that allocates that storage on
something like a heap.  Space for `var` would be allocated each time the
block is entered, and explicitly deallocated each time it's left -- and
the address of that storage could differ on each iteration.

> Otherwise it would be impossible to generate a single body of code for
> the loop. (In your example, the loop can be unrolled, and conceivably,
> each of the ten 'var's can have a different address. But for most
> loops that is not practical.)

Allocation for the block would update something like a frame pointer.
`var` would be accessed as an offset from the current value of the frame
pointer.

I don't suggest that this is a likely implementation, but it's perfectly
valid as far as the standard is concerned.  There have been compilers
that generate code that allocates "stack" storage from something like a
heap, but I doubt that they take it down to the block level.

[...]

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