On 9/13/07, Somu <[EMAIL PROTECTED]> wrote: > Hi list, > Recently i've been learning a bit of C programming. I used data > structures and then linked lists .. Does perl have this concept? > Linked list helps in good memory management. Does perl automates > memory management for us? Also, all the C variables are bound to be > declared at the top, so that the program can allot memory for them. > But this is not the case with perl where variables come and go with > scope. What if not enough memory is available? snip
In C you need linked lists because it is expensive and cumbersome to add elements to the middle of an array and because arrays must be contiguous blocks of memory. Perl does not have those problems (or at least they are hidden from you) so we tend not to use linked lists, but you can easily implement them in the same fashion as C does (replace C's structs with hashes and pointers with references). In ISO C 99 variables must be declared at the beginning of a block (in prior versions it had to be at the start of a function or globally). This is to make the job of the compiler writer easier. It has nothing to do with memory management. In Perl variables can be declared anywhere and only start using memory at that point. Variables stop using memory (although Perl may hold onto that memory to reuse later) when they no longer have any references. This scheme of garbage collection is called Reference Counting. It is a very fast scheme, but it is susceptible to leaks. The classic leak is one when you create a circular reference. Here is one example: { my $a; #count for $a's memory is 1; my $b = \$a; #count for $a's memory is 2, $b's memory is 1 $a = \$b; #count for $a's memory is 2, $b's memory is 2 } We are at the end of the block, so $a and $b go out of scope and therefore their reference counts go down by 1. $a goes out of scope so $a's ref count goes to 1 from 2. $b goes out of scope so $b's ref count goes to 1 from 2. At this point it is impossible for Perl to reuse those memory locations because they still have a ref count of 1 and there is no way to decrement the ref counts (there are no entries in the symbol table to that memory). Eventually the program will end and all memory locations held by Perl will be given back to the OS, so the leak won't persist past the program's end (unless there is a bug in the interpreter). As for what happens when not enough memory is available this is the last bit of the output for the following program: total: 3531 meg total: 3532 meg total: 3533 meg total: 3534 meg total: 3535 meg perl(257) malloc: *** vm_allocate(size=1052672) failed (error code=3) perl(257) malloc: *** error: can't allocate region perl(257) malloc: *** set a breakpoint in szone_error to debug Out of memory! #!/usr/bin/perl use strict; use warnings; my $meg = "a" x 1024 x 1024; my @a; #eight gig, adjust as neccessary for my $i (1 .. 1024 * 8) { push @a, $meg; print "total: $i meg\n"; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/