Recursion with pointer and memory leak
Tag : cpp , By : Yolanda N. Ceron
Date : March 29 2020, 07:55 AM
will be helpful for those in need "Could any one point out if following code can have a memory leak when its executed?" char *temp_string = new char[100]; // first assignment
[...]
if (number>=1000)
{
[...]
temp_string = convert_number(number,places);
|
Irrefutable pattern does not leak memory in recursion, but why?
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Let me first answer why mapAndSome can work good at all: What you see is (very likely) the effect of an optimization described by Philip Wadler in “ Fixing some space leaks with a garbage collector”. Short summary: If the garbage collector sees a thunk of the form fst x and x is already evaluated to the tuple constructor, e.g. (y,z), it will replace fst x by y, possibly freeing up z if it is not referenced anywhere else. In your code, the s' will, once the result of go is evaluated to a tuple and after one round of GCing, not keep a reference to the tuple but will be replaced by the accumulated parameter. foo_r2eT :: ([Type.Integer], Type.Integer)
foo_r2eT =
case $wgo_r2eP mapAndSum1 lvl2_r2eS
of _ { (# ww1_s2d7, ww2_s2d8 #) ->
(ww1_s2d7, ww2_s2d8)
}
case eqString ds_dyA lvl18_r2fd of _ {
False -> $wa_s2da new_s_a14o;
True ->
case ds1_dyB of _ {
[] ->
case Handle.Text.hPutStr2
Handle.FD.stdout lvl17_r2fc True new_s_a14o
of _ { (# new_s1_X15h, _ #) ->
Handle.Text.hPutStr2
Handle.FD.stdout lvl16_r2fb True new_s1_X15h
};
: ipv_sIs ipv1_sIt -> $wa_s2da new_s_a14o
}
lvl17_r2fc :: String
[GblId]
lvl17_r2fc =
case foo_r2eT of _ { (xs_Xqp, s_Xq9) ->
$w$cshowsPrec
0
(Data.List.sum_sum' xs_Xqp Data.List.genericDrop2)
([] @ Char)
}
lvl16_r2fb :: String
[GblId]
lvl16_r2fb =
case foo_r2eT of _ { (xs_apS, s_Xqp) ->
$w$cshowsPrec 0 s_Xqp ([] @ Char)
}
case eqString ds_dyA lvl8_r2f1 of _ {
False -> $wa2_s2dI w3_s2cF;
True ->
case ds1_dyB of _ {
[] ->
Handle.Text.hPutStr2
Handle.FD.stdout lvl7_r2f0 True w3_s2cF;
: ipv_sHg ipv1_sHh -> $wa2_s2dI w3_s2cF
}
} } in
lvl7_r2f0 :: String
[GblId]
lvl7_r2f0 =
case foo_r2eT of _ { (x_af6, y_af7) ->
show_tuple
(:
@ ShowS
(let {
w2_a2bY [Dmd=Just L] :: Type.Integer
w2_a2bY = lgo_r2eU mapAndSum1 x_af6 } in
\ (w3_a2bZ :: String) ->
$w$cshowsPrec 0 w2_a2bY w3_a2bZ)
(:
@ ShowS
(\ (w2_a2bZ :: String) ->
$w$cshowsPrec 0 y_af7 w2_a2bZ)
([] @ ShowS)))
([] @ Char)
}
case eqString ds_dyA (unpackCString# "bad?")
of _ {
False -> fail2_dyN realWorld#;
True ->
case ds1_dyB of _ {
[] ->
$
@ (Type.Integer, Type.Integer)
@ (IO ())
(System.IO.print
@ (Type.Integer, Type.Integer) $dShow_rzk)
($
@ ([Type.Integer], Type.Integer)
@ (Type.Integer, Type.Integer)
(Control.Arrow.first
@ (->)
Control.Arrow.$fArrow(->)
@ [Type.Integer]
@ Type.Integer
@ Type.Integer
sum'_rzm)
foo_rzl);
: ipv_szd ipv1_sze -> fail2_dyN realWorld#
}
} } in
w_r2f2 :: Type.Integer
w_r2f2 =
case foo_r2eT of _ { (x_aI1, y_aI2) ->
lgo_r2eU mapAndSum1 x_aI1
}
lvl9_r2f3 :: String -> String
[GblId, Arity=1]
lvl9_r2f3 =
\ (w2_a2bZ :: String) ->
$w$cshowsPrec 0 w_r2f2 w2_a2bZ
w1_r2f4 :: Type.Integer
w1_r2f4 = case foo_r2eT of _ { (x_aI6, y_aI7) -> y_aI7 }
lvl10_r2f5 :: String -> String
[GblId, Arity=1]
lvl10_r2f5 =
\ (w2_a2bZ :: String) ->
$w$cshowsPrec 0 w1_r2f4 w2_a2bZ
lvl11_r2f6 :: [ShowS]
[GblId]
lvl11_r2f6 =
:
@ ShowS lvl10_r2f5 ([] @ ShowS)
lvl12_r2f7 :: [ShowS]
[GblId]
lvl12_r2f7 = : @ ShowS lvl9_r2f3 lvl11_r2f6
lvl13_r2f8 :: ShowS
[GblId]
lvl13_r2f8 = show_tuple lvl12_r2f7
lvl14_r2f9 :: String
[GblId]
lvl14_r2f9 = lvl13_r2f8 ([] @ Char)
|
Memory leak in recursion
Tag : cpp , By : user157064
Date : March 29 2020, 07:55 AM
around this issue Compiling your code and running it in valgrind produces this as the first occurrence of your memory leak: ==20797== Invalid write of size 4
==20797== at 0x400BA0: Sort_And_count_split_Inv(int*, int, int, int) (in /home/test/TestCPP)
==20797== by 0x4009D4: Sort_And_count(int*, int, int) (in /home/test/TestCPP)
==20797== by 0x4009BC: Sort_And_count(int*, int, int) (in /home/test/TestCPP)
==20797== by 0x4009A2: Sort_And_count(int*, int, int) (in /home/test/TestCPP)
==20797== by 0x400921: main (in /home/test/TestCPP)
==20797== Address 0x5a1d0ec is 4 bytes after a block of size 8 alloc'd
==20797== at 0x4C2B800: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20797== by 0x400AE8: Sort_And_count_split_Inv(int*, int, int, int) (in /home/test/TestCPP)
==20797== by 0x4009D4: Sort_And_count(int*, int, int) (in /home/test/TestCPP)
==20797== by 0x4009BC: Sort_And_count(int*, int, int) (in /home/test/TestCPP)
==20797== by 0x4009A2: Sort_And_count(int*, int, int) (in /home/test/TestCPP)
==20797== by 0x400921: main (in /home/test/TestCPP)
tmp[k] = a[i];
|
Memory leak while using tail-recursion
Date : March 29 2020, 07:55 AM
I hope this helps you . I think as of right now, the answer is "the intermediate variable should be released, but it won't be". ES6 does talk about tail-position calls and even requires optimization:
|
memory leak in php with recursion
Date : March 29 2020, 07:55 AM
|