【发布时间】:2013-09-25 13:07:22
【问题描述】:
我经常想,
我知道我可以使用new 关键字在将相同指针作为参数传递给函数的同时创建指向对象实例的指针。就像我在下面的 Animation::newFrame 函数中一样,在下面的示例中给出。
但是,我也知道,作为一般规则,我有责任在使用 new 创建的内容上调用 delete。
所以当我这样调用 Frame 的构造函数时:
Frame* myFrame
= new Frame(new point(0,0), new point(100,100), new point(50,20));
最终释放我在上述函数调用中使用new 创建的 3 个点的内存的责任在哪里?
毕竟,以上 3 个新点并没有确切的名称供我称呼delete。
我一直在某种程度上假设它们属于被调用的函数的范围,并且它们只会超出函数的范围。然而,最近我一直在想,也许不是这样。
我希望我在这里已经足够清楚了。
提前致谢,
男人
struct Frame
{
public:
point f_loc;
point f_dim;
point f_anchor;
//the main constructor:: Creates a frame with some specified values
Frame(point* loc, point* dim, point* anchor)
{
f_loc = loc;
f_dim = dim;
f_anchor = anchor;
}
};
struct Animation
{
public:
vector<Frame*> frameList;
//currFrame will always be >0 so we subtract 1
void Animation::newFrame(int &currFrame)
{
vector<Frame*>::iterator it;//create a new iterator
it = frameList.begin()+((int)currFrame);//that new iterator is
//add in a default frame after the current frame
frameList.insert(
it,
new Frame(
new point(0,0),
new point(100,100),
new point(50,20)));
currFrame++;//we are now working on the new Frame
}
//The default constructor for animation.
//Will create a new instance with a single empty frame
Animation(int &currFrame)
{
frameList.push_back(new Frame(
new point(0,0),
new point(0,0),
new point(0,0)));
currFrame = 1;
}
};
编辑:我忘了提到这个问题纯粹是理论上的。我知道原始指针有更好的选择,例如智能指针。我只是想加深我对常规指针以及如何管理它们的理解。
上面的例子也是取自我的一个项目,它实际上是 C++/cli 和 c++ 混合(托管和非托管类),所以这就是为什么构造函数只接受 point* 而不是按值传递(point )。因为point是一个非托管结构,因此在托管代码中使用时,必须由我自己,程序员来管理。 :)
【问题讨论】:
-
该代码不可能编译。类型不匹配:f_loc = loc;也是-1。您应该尝试自己构建它。
-
这就是为什么这些东西应该按值传递(而不是首先由
new创建)。如果你用new创建它们,你必须在某个时候给它们打电话delete。 -
你为什么坚持在堆上分配对象(用
new)?为什么不使用临时对象,例如:Frame myFrame(point(0,0), point(100,100), point(50,20));。它更便宜,因为它避免了您观察到的昂贵的堆分配和内存分配问题。 -
@Andrzej 我经常这样问自己。我很久以前就创建了这些结构,并且自从它们在我的代码中被广泛使用以来就没有改变它们。他们接受
point*而不是point,原因我已经记不得了。您上面的评论是完全正确的,但我将这个问题作为一个理论问题提出,只是为了更深入地了解指针及其所有权。 -
您当前的代码也不是异常安全的。如果
new调用或point构造函数之一抛出异常,您将泄漏成功的构造函数。
标签: c++ memory-management scope