Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Perl: the Markov chain saw
 
PerlMonks  

delete hash element while traversing via values coredumped

by jesuashok (Curate)
on Jun 08, 2006 at 08:27 UTC ( #554235=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

%a = ( 'b' => {}, '' => {'c' => [{}]}, ); for(values %a) { delete $a{b} for(@{$_->{c}}) }
Output:-
Segmentation Fault (core dumped)
Could anyone explain me the reason why it has created a segmentation falut.

"Keep pouring your ideas"

Comment on delete hash element while traversing via values coredumped
Select or Download Code
Re: delete hash element while traversing via values coredumped
by gellyfish (Monsignor) on Jun 08, 2006 at 08:56 UTC

    It shouldn't coredump at all, and it doesn't for me. If you have a perl older than 5.8.1 then I would suggest you upgrade. If you have a newer one then I would use perlbug to submit this as a bug report.

    /J\

Re: delete hash element while traversing via values coredumped
by holli (Monsignor) on Jun 08, 2006 at 09:00 UTC
    It doesn't coredump for me (ActivePerl 5.8.8.817) but i get
    Use of freed value in iteration at dump.pl line 8.
    and the script dies.


    holli, /regexed monk/
Re: delete hash element while traversing via values coredumped
by ioannis (Curate) on Jun 08, 2006 at 09:03 UTC

    It is not very difficult if you enable warnings; it says "Use of freed value in iteration". Note that $a{b} is deleted in both iterations, yet on on of the iterations it tries to derefence its value of the key (the value is already freed, along with the key.) The program decides to exit with die() and does not crash.

Re: delete hash element while traversing via values coredumped
by davido (Archbishop) on Jun 08, 2006 at 16:15 UTC

    Who is giving you these snippets? Is someone putting you up to this?

    You've been around awhile here, so I'm sure by now you've read perlfaq4. But just in case, here's an excerpt from it that seems relevant and fairly direct:

    What happens if I add or remove keys from a hash while iterating over it?

    (contributed by brian d foy)

    The easy answer is "Don't do that!"

    If you iterate through the hash with each(), you can delete the key most recently returned without worrying about it. If you delete or add other keys, the iterator may skip or double up on them since perl may rearrange the hash table. See the entry for each() in perlfunc.

    Please tell us which version of Perl is segfaulting with the code snippet you provided. I am anxious to hear the answer.


    Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (16)
As of 2014-04-23 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (554 votes), past polls