When are pointers needed in oF and why?

Hello!

I’ve been learning c++ on and off for the past two years. I’m finally feeling like I’m getting a hang of the pointer syntax, but I still don’t understand why they seem to be required in certain cases, specifically in oF.

Making a vector of ofxDSHapPlayer , pushing individually defined ofxDSHapPlayer in and then trying to update them with a for loop caused an error, and the creator of that plugin said that was something they’d encountered before and to try doing it with pointers, which worked.

Likewise, when I tried to make a vector of ofFmodSoundPlayer to play multiple sounds at once, none of them or only the most recently loaded one would play. Again, I had to make an std::vector<ofFmodSoundPlayer*> to get it working.

Now, I would’ve thought that if you define an object and push it into a vector, it continues to exist and be accessible in memory via the vector, but unless you use pointers, it seems like the objects get lost or overridden by subsequent definitions.

Is this a c++ thing or a something specific to oF or to the addons in question? How can I know when to use pointers? I remember that ofxBox2d explicitly shows you to use pointers in the examples, but this is not the case for all addons.

one thing to think about is that c++ is generally pass by copy – when you call a function, such as
push_back(…) to a vector, the thing you pass in is copied. For core OF objects, we generally handle this well, so you can do the following,

ofImage blah;
blah.load(...);
myImages.push_back(blah);

what actually happening when you call push_back() is the blah object is getting copied, and the textures and pixel memory of the ofImage are getting recreated in a new object which gets added into a vector. that’s heavy and unless the object is coded correctly to deal with this kind of cloning, it’s not something all addons may handle well. if you instead of a vector of pointers, well then you are only passing around a pointer (alias) to where a thing is so the copy operation doesn’t matter.

Thank you @zach . I didn’t realize that push_back was copying. In that case, would a vector of references work also?

I’m used to the idea that, when you leave the scope of a variable, it stops existing, but pointers, at least, seem to allow you to hang on to them. (though pointers themselves seem need to be removed explicitly from memory with the delete keyword)

I think generally the problem with a vector of references is that the temporary variables you may create may go out of scope.

for (int i = 0; i < 10; i++){
ofImage temp;
temp.load(..);
myRefVector.push_back(&temp);
}

the issue is that temp goes out of scope and is deleted – so storing a reference to it doesn’t really help.

In general I do vectors of image like this – I will usually push back an unallocated object, then allocate in the vector

for (int i = 0; i < 10; i++){
ofImage temp;
myImages.push_back(temp);
myImages.back().load(....);
}

I’m sure there’s other smarter ways to do this, but it’s the way I’ve learned to do it.

1 Like

OK, that’s what I was wondering. So pointers will keep an out-of-scope object in memory, but references won’t. Thank you!

Hey @s_e_p , I don’t use pointers too often but when I do I use the “smart” kind like std::shared_ptr or sometimes std::unique_ptr. The smart ones don’t have to be explicitly removed from memory, which is the case for the c-style pointers. std::unique_ptr owns the object it points to, and does not allow copies to be made, which impacts how it can be used with a container. For lots of oF objects, a shared_ptr will work great!

Hi, @s_e_p !

It’s definitely a c++ thing - when you push an object into a vector, it’s copied into the vector. So if you later change the original object, the copy in the vector won’t be changed.

If you want the vector to contain a reference to the original object, you need to use pointers. That way, when you change the original object, the change will be reflected in the vector.

There’s no hard and fast rule for when to use pointers - it really depends on what you’re trying to achieve. In general, though, if you’re working with objects that you want to be able to change from outside the vector, you’ll need to use pointers.

But like I said, it really depends on what you’re trying to do. If you’re not sure, the best thing to do is experiment and see what works best in your particular situation.

Let me try to explain it to you in a way that might make more sense. When you define an object and push it into a vector, the object exists in memory as long as the vector exists. However, if you try to access the object after the vector is destroyed, then you will get an error because the object no longer exists in memory.

The same thing happens if you try to access an object after it has been overwritten by another object. This is why it’s important to use pointers when working with objects in vectors. By using pointers, you are essentially telling the computer that the object still exists even after the vector is destroyed or overwritten.

I hope this explanation makes sense and helps you understand when and why to use pointers in C++ programming. If not, feel free to ask for clarification or additional help from me or from other members of the “Open Frameworks” community.

1 Like

Hey, thanks for the breakdown. I understand the basic concept of pointers, but I was moreso wondering why vectors of some objects don’t work (making pointers necessary). I think I’ll mark @zach 's response as the solution.

1 Like