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


Groups > comp.lang.c > #59059

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

Date 2015-03-26 22:26 -0700
From Robbie Hatley <see.my.sig@for.my.address>
Organization Tustin Free Zone
Newsgroups comp.lang.c
Subject Re: Lexical Variables In C Loops (As Compared To Perl Loops)
References <ZK2dnYy7U9u2vpDInZ2dnUVZ57ydnZ2d@giganews.com> <mek71u$ai7$1@dont-email.me> <MPG.2f776e088e8f86712@news.eternal-september.org> <mengou$omv$1@dont-email.me>
Message-ID <H7ednfaII6GSd4nInZ2dnUVZ572dnZ2d@giganews.com> (permalink)

Show all headers | View raw


On 3/22/2015 3:47 PM, James Kuyper wrote:

> Can you refer to $var after the end of the loop? If only the memory
> persists, rather than access to that memory through the variable name,
> then you can't properly say that the variable persists. If you can
> access the memory through $var after the loop, which of the ten
> different addresses given does it refer to?

Perl is ref-counted, so if you repeatedly alter a variable, save
a reference, alter the variable, save a reference, etc, during a
loop, then multiple "snapshots" of the variable will be created.
When the loop exists, the variable goes out of scope. BUT, the
snapshots remain, and the references you saved all point towards
those valid referents. Only when the references themselves go out
of scope will the referents be trashed by the garbage collector.

> I think that he C equivalent of this perl code would not look like
> Robbie Hatley's original code, but more like this:
>
> for(int i=1; i<=10; i++)
> {
>      int *var = malloc(sizeof *var);
>      *var = i;
>      array[i] = var;
> }

That's close. But there's no exact way of doing Perl-style
variables in C, because C "auto" variables go "poof" when
their enclosing scope is exited. Whereas, Perl "my" variables
do NOT go "poof" on going out-of-scope if their ref count
is > 0; the variable can no longer be referred-to by name,
but the underlying referent can be read/written by reference.

As for C's "malloc" and C++'s "new", I rarely use either,
because creating things on-the-fly requires that one remember
to DESTROY them on-the-fly when they're no longer needed,
lest the computer's RAM get clogged with zombies.

Which maybe explains why certain software (Mozilla's
Firefox comes to mind; and Disney's "Toontown Online"
game client was another example) cause one's RAM usage
to slowly, inexorably increase till RAM is full and
hard disk is thrashing. Too many malloc's without free's?

The only garbage collector in C is the programmer.
So if he keeps slopping garbage around without cleaning it up,
it accumulates. Much to the anger of the end user who has to
keep pushing the "Reset" button because his machine keeps
freezing up.



-- 
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
http://www.well.com/user/lonewolf/
https://www.facebook.com/robbie.hatley

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