Pointers, dynamic allocated memory and garbage collections

Hi there,
I’m coming from Processing, and trying to learn openFrameworks, in particular because I would like to improve my C++ knowledge which is quite basic. So, I have a simple question about pointers and dynamic allocated memory, since in Processing you never think about that. I just want to check that I’m “starting with the right foot”. :smile:
Suppose then I have the following function

void add(ofVec3f v1, ofVec3f v2){
ofVec3f dir = v1 + v2 ;
//do something with dir;
} 

So, as far as I understand, if I do add(w1, w2), what happens is that a copy of w1 and w2 will be made to use inside the scope of add(), an object of type ofVec3f, dir, will be assigned to the sum of v1 and v2, and at the end of the function, v1, v2, and dir will be removed, and their memory allocation freed.
On the other hand, if I have the following function

void add(){
ofVec3f* dir = new ofVec3f(1.0, 0.0, 3.0);
}

a pointer dir of type ofVec3f is created, and it will point to the dynamic allocated memory where the object ofVec3f(1.0, 0.0, 3.0) lives. At the end of the function, the pointer dir will be removed, but the memory allocation will not be freed, unless I add the line “delete dir” at the end of the function.
Is all of this correct?
If so, do you have any best practice rules, sort of guidelines for a beginner to avoid unnecessary memory wasting while using openFrameworks?
Thanks a lot for reading,
Alessandro

1 Like

in general don’t use pointers, most of the time you don’t need them. also you can avoid copies by using references so in:

void add(ofVec3f v1, ofVec3f v2){
    ofVec3f dir = v1 + v2 ;
    //do something with dir;
}

you can do:

void add(ofVec3f & v1, ofVec3f & v2){
    ofVec3f dir = v1 + v2 ;
    //do something with dir;
}

which will pass v1 and v2 as a reference instead of copying them, for something as small as an ofVec3f it won’t make much difference but if you are passing something like an image then it’s important. the most correct will actually be:

void add(const ofVec3f & v1, const ofVec3f & v2){
    ofVec3f dir = v1 + v2 ;
    //do something with dir;
}

which states that v1 and v2 won’t be changed inside the add function and allows you to call the function like:

add(ofVec3f(0,0,0),ofVec3f(1,1,1));

without the const you would need to do:

ofVec3f v1(0,0,0);
ofVec3f v2(1,1,1);
add(v1,v2)

at some point you might need to keep a reference to an object in several places, that’s usually the only case where you need pointers, the easiest in those cases is to use a shared_ptr which auto deletes itself when nobody is using it, similar to garbage collection except that the objects are deleted immediately when the last reference dissapears and not when the garbage collector decides to delete them:

shared_ptr<MyClass> ptr(new MyClass);

now you can use ptr with the same syntax as a raw pointer, pass it around share it among several objetcs and not care about deleting it

i’ve written a chapter on memory in c++ for the OF book we are preparing: https://github.com/openframeworks/ofBook/blob/master/16_memory/chapter.md that talks about all this things. the images are currently broken on the github render but should be understandable without them

6 Likes

Hi Arturo,
thanks a lot for the very clear answer, this is gonna be very useful. I will give a good read to the chapter you linked.
Best,
Alessandro