我正在对树结构进行插入/搜索,但在释放内存时遇到了问题。当我在return语句之前的代码末尾添加“ delete currentNode”时,在运行后不久我会遇到此错误:
library("plm" )
library(RStata)
data("Cigar", package = "plm")
z <- Cigar[ Cigar$year %in% c( 63, 73) , ]
options("RStata.StataPath" = "\"C:\\Program Files (x86)\\Stata14\\StataSE-64\"")
options("RStata.StataVersion" = 14)
# Stata fe
stata_do1 <- '
xtset state year
xtreg sales pop, fe
'
stata(stata_do1, data.out = TRUE, data.in = z)
#> .
#> . xtset state year
#> panel variable: state (strongly balanced)
#> time variable: year, 63 to 73, but with gaps
#> delta: 1 unit
#> . xtreg sales pop, fe
#>
#> Fixed-effects (within) regression Number of obs = 92
#> Group variable: state Number of groups = 46
#>
#> R-sq: Obs per group:
#> within = 0.0118 min = 2
#> between = 0.0049 avg = 2.0
#> overall = 0.0048 max = 2
#>
#> F(1,45) = 0.54
#> corr(u_i, Xb) = -0.3405 Prob > F = 0.4676
#>
#> ------------------------------------------------------------------------------
#> sales | Coef. Std. Err. t P>|t| [95% Conf. Interval]
#> -------------+----------------------------------------------------------------
#> pop | -.0032108 .0043826 -0.73 0.468 -.0120378 .0056162
#> _cons | 141.5186 18.06909 7.83 0.000 105.1256 177.9116
#> -------------+----------------------------------------------------------------
#> sigma_u | 34.093409
#> sigma_e | 15.183908
#> rho | .83448264 (fraction of variance due to u_i)
#> ------------------------------------------------------------------------------
#> F test that all u_i=0: F(45, 45) = 8.91 Prob > F = 0.0000
# R
z2 <- pdata.frame( z , index=c("state", "year") )
coef( plm( sales ~ pop , data= z2 , model="within" ) )
#> pop
#> -0.003210817
# Stata re
stata_do2 <- '
xtset state year
xtreg sales pop, re
'
stata(stata_do2, data.out = TRUE, data.in = z)
#> .
#> . xtset state year
#> panel variable: state (strongly balanced)
#> time variable: year, 63 to 73, but with gaps
#> delta: 1 unit
#> . xtreg sales pop, re
#>
#> Random-effects GLS regression Number of obs = 92
#> Group variable: state Number of groups = 46
#>
#> R-sq: Obs per group:
#> within = 0.0118 min = 2
#> between = 0.0049 avg = 2.0
#> overall = 0.0048 max = 2
#>
#> Wald chi2(1) = 0.40
#> corr(u_i, X) = 0 (assumed) Prob > chi2 = 0.5257
#>
#> ------------------------------------------------------------------------------
#> sales | Coef. Std. Err. z P>|z| [95% Conf. Interval]
#> -------------+----------------------------------------------------------------
#> pop | -.0006838 .0010774 -0.63 0.526 -.0027955 .001428
#> _cons | 131.1398 6.499511 20.18 0.000 118.401 143.8787
#> -------------+----------------------------------------------------------------
#> sigma_u | 30.573218
#> sigma_e | 15.183908
#> rho | .80214841 (fraction of variance due to u_i)
#> ------------------------------------------------------------------------------
# R random
coef(plm(sales ~ pop,
data=z,
model="random",
index=c("state", "year")))
#> (Intercept) pop
#> 1.311398e+02 -6.837769e-04
当我不包括delete currentNode时,我的程序输出搜索结果的时间要比产生分段错误之前“删除”的时间长。我想知道我需要在哪里为currentNode分配内存,以便不会耗尽内存。
代码如下:
Error in `./a.out': double free or corruption
答案 0 :(得分:2)
您正在为新的Node
对象分配内存,但是永远不要使用它。相反,您立即使用指向root
的指针覆盖了指向新对象的指针。
调用delete currentNode
时,您不会删除新对象,但是currentNode
指向函数末尾的所有内容。
因此,不用写myEntry::Node *currentNode = new myEntry::Node;
,只需写myEntry::Node *currentNode = root;
。
这样,您无需分配任何内存,因此不必释放任何内存。
答案 1 :(得分:1)
设置currentNode = root;
时,将丢失对创建的新节点的引用,从而导致内存泄漏。