[tpm] Weird arrary reference bahaviour

Liam R E Quin liam at holoweb.net
Wed Oct 31 22:53:59 PDT 2007


On Wed, 2007-10-31 at 21:42 -0400, Indy Singh wrote:

> > At the end of each iteration of the loop, @f falls out of scope.
> I would have thought that @f falls out of scope _after_ the closing 
> brace, not while still inside it.  But you seem to be right.

Perl is like C in this regard.

for (i = 0; i < 7; i ++) {
  char *boy = strdup("Simon");
  printf("%d %x\n", i, boy);
}

will give different addresses each time round the loop.

The variable is created anew each time round (and in this case
there's a memory leak, of course, because we never free the boy).

for (i = 0; i < 7; i ++) {
  char *boy = strdup("Simon");
  printf("%d %x\n", i, boy);
  free(boy);
}

will work better in C.

Liam

-- 
Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org www.advogato.org



More information about the toronto-pm mailing list