我写了一个程序如下:
trait MyBaseTrait
type MyCP = Boolean :+: Long :+: ... :+: String :+: MyBaseTrait
case class ValidSimple(b: Boolean, s: String) extends MyBaseTrait
case class ValidNested(s: String, vs: ValidSimple) extends MyBaseTrait
case class NotOfBaseTrait(l: Long)
case class InvalidNested(s: String, vs: NotOfBaseTrait) extends MyBaseTrait // => compile error
如果我使用if(i = 0)
{ vector<int> data;
vector<int>::iterator it;
data.push_back(2);
data.push_back(3);
sort(data.begin(),data.end());
it = data.begin();
}
if(i = 0)
{ vector<int> data;
vector<int>::iterator it;
data.push_back(5);
data.push_back(1);
sort(data.begin(),data.end());
it = data.begin();
}
两次,它会自动释放吗?我应该释放内存吗?
答案 0 :(得分:4)
当变量超出范围时,会自动删除局部变量的内存分配。
if(i == 0)
{
std::vector<int> data; //local variable
}
//allocation for 'data' is deleted automatically
if(i == 0)
{
std::vector<int> data; //this is not the same vector as above
//in fact, the vector above no longer exists at this point
}
//allocation for 'data' is deleted automatically