Pages: [1]
Author Topic: Vector & List examples  (Read 8235 times)
abstractmachine
Aix-en-Provence, France

Posts: 31

Gravatar


WWW
Vector & List examples
« on: April 27, 2008, 02:01:55 PM »

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?
« Last Edit: January 01, 1970, 01:00:00 AM by abstractmachine » Logged
pyramind
Turkey

Posts: 31

Gravatar


(No subject)
« Reply #1 on: May 02, 2008, 02:21:56 AM »

These are great, clean examples of functionality. Thanks.
« Last Edit: January 01, 1970, 01:00:00 AM by pyramind » Logged

- To err is human but to really foul things up requires a computer.
otherside

Posts: 94

Gravatar


(No subject)
« Reply #2 on: May 02, 2008, 08:35:09 PM »

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

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

To look like:

Code:
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:

Code:
delete[] myBall;  // this will delete all nBalls
« Last Edit: January 01, 1970, 01:00:00 AM by otherside » Logged
abstractmachine
Aix-en-Provence, France

Posts: 31

Gravatar


WWW
Wiki example
« Reply #3 on: May 02, 2008, 11:05:15 PM »

Quote from: "otherside"
Hi,
In your example on the ofWiki


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.
« Last Edit: January 01, 1970, 01:00:00 AM by abstractmachine » Logged
abstractmachine
Aix-en-Provence, France

Posts: 31

Gravatar


WWW
(No subject)
« Reply #4 on: May 08, 2008, 04:45:23 PM »

Based on discussion over here (http://forum.openframeworks.cc/index.php/topic,641.0.html), 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?
« Last Edit: January 01, 1970, 01:00:00 AM by abstractmachine » Logged
Pages: [1]
 
Jump to:  

Powered by SMF 1.1.15 | SMF © 2011, Simple Machines
kinect

viagra priser