Can modify array during iteration

e$B1sF#$H?=$7$^$9!#e(B

1.9
e$B$G0J2<$N$h$&$K$9$k$H%$%F%l!<%HCf$NG[Ns$r=q$-49$($k$3$H$,$G$-$^$9!#e(B

a = [0, 1, 2]
a.each do
a.each {}
a.clear
end
p a #=> []

e$BFbB&$Ne(B each e$B$,=*$o$k;~!"e(Ba e$B$Ne(B ARY_ITERLOCK
e$B$,2rJ|$5$l$F$7$^$&$?$a$G$9!#e(B

[email protected]$9$k$?$a$K$O!"e(BARY_ITERLOCK
e$B$,%m%C%/$5$l$F$$$k2s?t$rJ]B8$7!"e(B
e$B$=$l$,e(B 0
[email protected]$1=q$-49$($rG’$a$k$h$&$K$7$J$$$H$$$1$J$$$H;W$$e(B
e$B$^$9!#e(B

e$B4XO"$7$F!“0J2<$N$h$&$K$9$k$HMn$A$^$9!#e(BArray#map!
e$B$,!”%$%F%l!<%HCf$Ke(B
e$B=q$-49$($i$l$J$$$3$H$rA0Ds$K$7$F$$$k$3$H$,860x$G$9!#e(B

$ ./ruby -e ’
a = (0 … 100000).to_a
a.map! do |i|
if i == 99999
a.each { }
a.clear
end
i
end

-e:3: [BUG] Segmentation fault
ruby 1.9.0 (2008-01-23 revision 0) [i686-linux]

– control frame ----------
c:0004 p:---- s:0010 b:0010 l:000009 d:000009 CFUNC :map!
c:0003 p:0018 s:0007 b:0007 l:000006 d:000006 TOP -e:3
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH :inherited
c:0001 p:0000 s:0002 b:0002 l:000001 d:000001 TOP :17

DBG> : “-e:3:in `’”
– backtrace of native function call (Use addr2line) –
0x80f04a5
0x810f3ce
0x810f42b
0x80bf860
0xffffe440
0x80598b1
0x80fabf5
0x80eecc3
0x80ea5ee
0x80edeb4
0x80ee1ed
0x805a6f9
0x805e840
0x8058121
0xb7dc1ea8
0x8058031

e$B%"%!<%H$7$^$7$?e(B

e$B0J2<$O%"%!<%H$9$kLdBj$KBP$9$k%Q%C%A$G$9!#e(B

Index: array.c

— array.c (revision 15191)
+++ array.c (working copy)
@@ -1644,7 +1644,7 @@

 rb_ary_modify(ary);
 for (i = 0; i < RARRAY_LEN(ary); i++) {
  • RARRAY_PTR(ary)[i] = rb_yield(RARRAY_PTR(ary)[i]);
  • rb_ary_store(ary, i, rb_yield(RARRAY_PTR(ary)[i]));
    }
    return ary;
    }

[email protected]$G$9!#e(B

At Thu, 24 Jan 2008 00:03:24 +0900,
Yusuke ENDOH wrote in [ruby-dev:33328]:

[email protected]$9$k$?$a$K$O!"e(BARY_ITERLOCK e$B$,%m%C%/$5$l$F$$$k2s?t$rJ]B8$7!"e(B
e$B$=$l$,e(B 0 [email protected]$1=q$-49$($rG’$a$k$h$&$K$7$J$$$H$$$1$J$$$H;W$$e(B
e$B$^$9!#e(B

e$B4XO"$7$F!“0J2<$N$h$&$K$9$k$HMn$A$^$9!#e(BArray#map! e$B$,!”%$%F%l!<%HCf$Ke(B
e$B=q$-49$($i$l$J$$$3$H$rA0Ds$K$7$F$$$k$3$H$,860x$G$9!#e(B

ARY_ITERLOCKe$B$Oe(Barray.ce$B0J30$G$O8+$($^$;$s$,!"e(Bcollect_bang_i()e$B0J30e(B
e$B$Oe(Brb_ary_store()e$B$r;H$C$F$$$k$N$G!"5U$Ke(BARY_ITERLOCKe$B$O$$$i$J$$$N$Ge(B
e$B$O$J$$$+$H$$$&5$$,$7$^$9!#e(B

e$BF1$8$/e(BARY_SORTLOCKe$B$b!“JQ99$9$k$H$-$He(Bsorte$BCf$K%V%m%C%/$+$iJV$C$?8ee(B
e$B$NN>J}$G%A%’%C%/$7$F$$$^$9$,!”$3$l$b8e<T$Ne(Bary_sort_check()[email protected]$1$Ge(B
e$B=<J,$G$O$J$+$m$&$+$H!#e(B

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:33343] Re: can modify array during iteration”
on Thu, 24 Jan 2008 13:05:43 +0900, Nobuyoshi N.
[email protected] writes:

|ARY_ITERLOCKe$B$Oe(Barray.ce$B0J30$G$O8+$($^$;$s$,!"e(Bcollect_bang_i()e$B0J30e(B
|e$B$Oe(Brb_ary_store()e$B$r;H$C$F$$$k$N$G!"5U$Ke(BARY_ITERLOCKe$B$O$$$i$J$$$N$Ge(B
|e$B$O$J$$$+$H$$$&5$$,$7$^$9!#e(B

e$B$H$$$&$3$H$O!"e(Bcollect_bang_i()e$B$G$be(Brb_ary_store()e$B$r;H$($Pe(B
ITERATEe$B$bMW$i$J$$$H$$$&$3$H$+!#$=$&$7$A$c$$$^$7$g$&$+!#e(B

|e$BF1$8$/e(BARY_SORTLOCKe$B$b!“JQ99$9$k$H$-$He(Bsorte$BCf$K%V%m%C%/$+$iJV$C$?8ee(B
|e$B$NN>J}$G%A%’%C%/$7$F$$$^$9$,!”$3$l$b8e<T$Ne(Bary_sort_check()[email protected]$1$Ge(B
|e$B=<J,$G$O$J$+$m$&$+$H!#e(B

qsort()e$B$r8F$s$G$k4V$KG[Ns$ND9$5$,$o$+$C$F$7$^$C$Fe(Brealloc()e$B$,e(B
e$B8F$P$l$?$j$9$k$H$^$:$$$+$i$8$c$J$$$G$7$g$&$+!#$3$A$i$Oe(B
rb_str_new4()e$BAjEv$N;E3]$1$r;H$&<j$b$"$j$^$9$h$M!#e(B

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs