Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Segmentation fault

by bloonix (Monk)
on Jun 25, 2007 at 20:05 UTC ( #623239=perlquestion: print w/replies, xml ) Need Help??

bloonix has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'm wondering about the following code snippet that triggers a segmentation fault if the script exit.
my $i = 30000; my %h = (new => {}); my $r = \%h; for (0..$i) { print $_, " "; $r = $r->{new}; $r->{new} = {}; } print "end\n";

...29998 29999 30000 end Segmentation fault (core dumped)
I test it with different iterations and from 24.000 the segfault is produced definitely. If I try to undef %h the segfault is produced immediate.
my $i = 30000; my %h = (new => {}); my $r = \%h; for (0..$i) { print $_, " "; $r = $r->{new}; $r->{new} = {}; } undef %h; print "end\n";

Output: 29837 29838 29839 29840 Segmentation fault (core dumped)
My question is now why that happend? Do you have an answer?


Edit: g0n - changed pre tags to code tags

Replies are listed 'Best First'.
Re: Segmentation fault
by Joost (Canon) on Jun 25, 2007 at 20:11 UTC
      Doesn't it seem like perl should have a cleaner way of recovering from this?
      $SIG{__ATROOSS__} = sub { die "about to run out of stack space!!" };

      This and the regexp one have always bugged the crap out of me.


        The problem is there is no portable way to determine that you are about to run out of stack space...

        We're not surrounded, we're in a target-rich environment!
      Hello Joost, big thanks for your fast answer. Yes, if I increase the ulimit the script runs very fine without a stack overflow. Do you know also why the segfault is produced first if the script die and not immediate?
        Do you know also why the segfault is produced first if the script die and not immediate?
        The stack-overflow is triggered when the top-level hash goes out of scope. In your example that is when the script ends, but that's just coincidental. If your hash goes out of scope before that, and your stack-size isn't big enough, you'll get a segfault at that point:

        { my $i = 60000; my %h = (new => {}); my $r = \%h; for (0..$i) { print $_, " "; $r = $r->{new}; $r->{new} = {}; } print "end 1\n"; } # <-- hash goes out of scope; segfault if stacksize is too low print "end 2\n";
        I'm not familiar with the implementation of perl's garbage collector, but I've seen this problem once before. In that case as in this one, the problem was caused by a program letting a long linked list (i.e. item a has a reference to item b which has a reference to item c etc. etc.) go out of scope.

        If you're really careful, you can clean up the linked list before it goes out of scope and prevent this error:

        { my $i = 60000; my %h = (new => {}); my $r = \%h; my $end = $r; # end of the list for (0..$i) { print $_, " "; $r = $r->{new}; $r->{new} = {}; $r->{new}->{old} = $r; # keep back-references $end = $r->{new}; } print "end 1\n"; while ($end) { # delete the list from the end delete $end->{new}; $end = delete $end->{old}; } } print "end 2\n";

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://623239]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2022-01-19 10:16 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (55 votes). Check out past polls.