我想删除FOR循环末尾的指针以避免内存泄漏。但是在FOR循环的中间,它可能会遇到条件if (i > maxHour / 2) continue;
并从头开始继续。为了避免内存泄漏,我试图检查if (NULL != p) delete[] p;
,但程序无法编译。我不确定这行if (NULL != p) delete[] p;
int main() {
int maxHour = 8760;
int *p;
for (int i = 0; i < maxHour; i++) {
if (NULL != p) delete[] p;
p = new int[100];
//do some things 1
if (i > maxHour / 2) continue;
//do some things 2
delete[] p;
}
return 0;
}
答案 0 :(得分:3)
有几个问题。
首先,您需要将p
初始化为nullptr
,以便第一次迭代不会尝试访问并释放未初始化的指针。
其次,在循环结束时删除指针后,需要将其设置为nullptr
,以便下一次迭代不会再尝试释放它。
最后,你需要在循环之后释放它,以防最后一次迭代点击continue
语句,以便它跳过delete
。
int main() {
int maxHour = 8760;
int *p = nullptr;
for (int i = 0; i < maxHour; i++) {
delete[] p;
p = new int[100];
//do some things 1
if (i > maxHour / 2) continue;
//do some things 2
delete[] p;
p = nullptr;
}
delete[] p;
return 0;
}
您不需要if (NULL != p)
,因为delete
会自动检查空指针并且不执行任何操作。
另一种方法是在继续之前删除指针。但是,如果您有许多不同的代码分支包含continue
,这可能不方便重复。
int main() {
int maxHour = 8760;
int *p;
for (int i = 0; i < maxHour; i++) {
p = new int[100];
//do some things 1
if (i > maxHour / 2) {
delete[] p;
continue;
}
//do some things 2
delete[] p;
}
return 0;
}
答案 1 :(得分:2)
对于这样的任务,智能指针#include <iostream>
#include <memory>
struct A
{
~A() { std::cout << "A::~A()" << std::endl; }
};
int main()
{
const size_t N = 10;
for ( size_t i = 0; i < N; i++ )
{
std::unique_ptr<A[]> p( new A[2] );
if ( i % 2 )
{
std::cout << "\nContinue" << std::endl;
continue;
}
std::cout << "\nIteration" << std::endl;
}
return 0;
}
是合适的。
这是一个示范程序
Iteration
A::~A()
A::~A()
Continue
A::~A()
A::~A()
Iteration
A::~A()
A::~A()
Continue
A::~A()
A::~A()
Iteration
A::~A()
A::~A()
Continue
A::~A()
A::~A()
Iteration
A::~A()
A::~A()
Continue
A::~A()
A::~A()
Iteration
A::~A()
A::~A()
Continue
A::~A()
A::~A()
程序输出
df<- structure(list(Pama1 = structure(c(2L, 5L, 3L, 5L, 3L, 3L, 3L,
3L, 3L, 4L, 1L), .Label = c("", "DD1", "n/a", "PAMANA", "zf"), class = "factor"),
X = structure(c(11L, 3L, 10L, 2L, 4L, 5L, 6L, 7L, 8L, 9L,
1L), .Label = c("", "116", "12", "138", "197", "219", "224",
"230", "280", "85", "Start1"), class = "factor"), X.1 = structure(c(11L,
10L, 2L, 4L, 3L, 5L, 8L, 6L, 7L, 9L, 1L), .Label = c("",
"101", "145", "199", "222", "227", "233", "238", "331", "89",
"End1"), class = "factor"), Pama2 = structure(c(2L, 4L, 4L,
4L, 3L, 4L, 4L, 6L, 5L, 1L, 1L), .Label = c("", "DD2", "GGTR",
"n/a", "PAMANA", "T_reg"), class = "factor"), X.2 = structure(c(9L,
2L, 2L, 8L, 3L, 4L, 5L, 6L, 7L, 1L, 1L), .Label = c("", "1",
"115", "208", "214", "232", "376", "85", "Start2"), class = "factor"),
X.3 = structure(c(10L, 8L, 2L, 9L, 3L, 4L, 5L, 6L, 7L, 1L,
1L), .Label = c("", "15", "195", "229", "231", "362", "577",
"76", "86", "End2"), class = "factor"), Pama3 = structure(c(1L,
3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("DD3",
"GGTR", "n/a"), class = "factor"), X.4 = structure(c(10L,
1L, 7L, 8L, 9L, 9L, 2L, 3L, 4L, 5L, 6L), .Label = c("1",
"129", "136", "153", "166", "178", "48", "65", "66", "Start1"
), class = "factor"), X.5 = structure(c(10L, 6L, 7L, 8L,
3L, 9L, 1L, 2L, 4L, 4L, 5L), .Label = c("131", "138", "144",
"168", "180", "34", "51", "70", "79", "End2"), class = "factor")), .Names = c("Pama1",
"X", "X.1", "Pama2", "X.2", "X.3", "Pama3", "X.4", "X.5"), class = "data.frame", row.names = c(NA,
-11L))
正如所见,内存被正确删除,与是否执行了continue语句无关。
您可以像使用数组一样使用带有此智能指针的下标运算符。