I've often wondered,
I know I can create a pointer to an instance of an object at the same time as passing that same pointer as an argument to a function, using the new
keyword. Like I have below in the Animation::newFrame
function, given in the example below.
However, I'm also aware that as a general rule I'm responsible for calling delete
on things that I create using new
.
So when I call Frame's constructor like so:
Frame* myFrame
= new Frame(new point(0,0), new point(100,100), new point(50,20));
Where does the responsibility to eventually free the memory for the 3 points I have created using new
in the above function call lie?
After all, the above 3 new points don't exactly have names for me to call delete
on.
I've always sort-of assumed that they belong in the scope of the function they're called in, and they will simply go out of scope with the function. However, lately I've been thinking maybe that is not so.
I hope I've been clear enough here.
Thanks in advance,
Guy
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;
}
};
EDIT: I forgot to mention that this question is purely theoretical. I am aware that there are much better alternatives to raw pointers such as smart pointers. I'm asking simply to deepen my understanding of regular pointers and how to manage them.
Also the example above is taken from a project of mine that is actually C++/cli and c++ mixed (managed and unmanaged classes), so that is why the contructor only accepts point*
and not passing by value (point
). Because point
is an unmanaged structure, and therefore when used within managed code, must be managed by myself, the programmer. :)
new
in the first place). If you create them withnew
you have to calldelete
on them at some point.new
)? Why not use temporary objects, like:Frame myFrame(point(0,0), point(100,100), point(50,20));
. It is cheaper because it avoids expensive heap allocation and memory allocation issues you observed.point*
and notpoint
for reasons I can no longer remember. Your comment above is completely correct, but I pose this question as a theoretical one, simply for the sake of a deeper understanding of pointers and their owenership.new
calls orpoint
constructors throws an exception you will leak the one(s) that succeeded.