Quote:
| Originally Posted by Aliii If I make an istance of it(CVector *V= new CVector()) should I call delete to V when I dont need it anymore? |
Yes, of course. Though what's much more important is that you delete V before it goes out of scope or before you use it in another new, either of which will cause a memory leak. But if V is global and hence will never go out of scope and will never get reused, then not deleting it when you no longer need it doesn't really cause any problem, but rather is simply wasteful.
Quote:
| Originally Posted by Aliii coord[3] is stored on the stack and its deleted automatically I guess. |
Huh??? Whatever gave you
that idea?
coord[3] is part of the CVector object you created. Assuming that you instantiated that CVector object with a new, then coord[3] is part-and-parcel of that object, which resides in the heap.
Why do you think that it's in the stack? The
pointer V may be in the stack if it's a local variable, but what it points to isn't.
Quote:
| Originally Posted by Aliii Also what if I allocate coord[3] in the CVector() constructor with new, then I make a CVector[100] array(outside of the constructor). Will those 100 floats be stored on the heap or the stack? |
So I assume you're talking about declaring coord as
float *coord; and that in the CVector constructor you will have something like:
coord = new float[3];
OK, think about it. Whenever you new something, where was that new something allocated? From the heap, right? The only things that are on the stack are directly related to function calls and include the argument list and local variables, though not local variables declared as static, which are stored in the read-write data segment along with the global variables.
Function data -- stack.
new and delete -- heap.
It's really that simple.