Vector & List examples

Howdy.

We’ve started using OF seriously in my Atelier this semester and now have to start building tutorials, examples, and so on – like yeasterday. I especially need to cover the bread & butter methods and coding styles we already use daily in Processing. One of the major ones being dynamic arrays, which we pretty much needed from day 1.

So Guillaume Stagnaro and I have been chatting over the past day or two over a basic vector example, followed by a list example. You can find them here (ignore the French explanation, the code comments are in English):

http://www.ecole-art-aix.fr/article3011.html

So I now wanted to put this up onto the wiki. I was thinking of adding it at the end of the Ooops! Tutorial:

http://wiki.openframeworks.cc/index.php-…-2B-Classes

Any better ideas?

Also, should I just start adding tutorials into the Wiki, or should I clear it with everyone on the forum first?

These are great, clean examples of functionality. Thanks.

Hi,
In your example on the ofWiki, I would modify:

  
  
ofBall **myBall;  
myBall = new ofBall*[nBalls];   // an array of pointers for the objects  
  

To look like:

  
  
ofBall *myBall;  
myBall = new ofBall[nBalls];  
  

for a couple of reasons.

  1. It’s saves a dereference when you try to get the Nth ball
  2. This is what has become convention in C++ programming

One thing to be _very_ careful of here is that you’re not using the “typical” new operator. Instead you’re using the array version of the new operator and you have to correspondingly call the array version of the delete operator else you will get memory leaks since only the first object in the array will be deallocated. The delete operation will look like:

  
  
delete[] myBall;  // this will delete all nBalls  
  

[quote author=“otherside”]Hi,
In your example on the ofWiki[/quote]

Actually, I didn’t write the pointer-based example, nor that tutorial (which is really good by the way), so I don’t want to get any credit where none is due.

That said, when I saw it I thought, hmmm, there’s got to be another way that my students could understand, since they all know dynamic arrays in Java – it’s our daily bread and butter – but they don’t know pointers very well and would probably flee in a terrified panic. So I wrote the Thingy examples (which are basically ofBalls but I like Thingies better ;-)) just to give them an idea on how to do what we already do in Processing.

Based on discussion over here (http://forum.openframeworks.cc/t/vectors-as-a-substitute-for-arraylists/641/0), I’ve been exploring using vectors of pointers instead of vectors that copy objects.

It works great (no copy constructors) but the problem is that this solution is far more complex than just using a simple vector, because it involves using new, and therefore delete, as well as the added complexity of dereferencing pointers of dereferenced pointers (iterators are just a form of pointer) – which is totally unworkable (in my opinion) for even intermediate programmers because it requires far too much knowledge.

On the other hand, using the vector<> and list<> examples, as in my two examples online, is unworkable in the real world because of the copies that are made of the object when adding them to the collector, thereby bringing in the problem of copy constructors.

So this is all kinda ugly. Should we make it all easier by introducing a wrapper that might simplify the process, along the lines of Processing’s append(), expand() and shorten() methods?