Your Opinions, General Stuff

Started by
22 comments, last by Stompy9999 19 years, 2 months ago
return 0; return 1; which one is failure and which one is success? I return 0 from main if nothing goes wrong but a lot of other things use 0 as a fail flag. Calling a function like if(!func() ) { /*error*/ }. And is using a linked list too much for animation purposes with a sprite? Having the last frame loop back to the first frame instead of iterating over an array?
Advertisement
First, main should return 0 if there are no errors.

Second, with a linked list, you don't know how the data will end up laid out in memory. However, the overhead of iterating through a linked list is probably not significantly different from the overhead of iterating through an array. Therefore, I would recommend an array because it will guarantee good spatial locality.
I've always used 0 as failure. !function seems much more like "failure" to me.
Yes, I agree !function seems like failure, like testing for NULL, and a lot of code seems to use that.

But returning non-zero for failure opens up the possibility for different errors. Like AUDIO_ERR=1, INIT_ERR=2, SUPER_BAD_ERR=4. Whereas success usually comes in one flavor.

For the animation question, I would use an array, as a linked list seems overkill. Afterall, you know ahead of time how many frames are in the animation sequence. But my friend says there is no performance difference, and doesn't want to keep track of the array and the counter, when you can just deference the next element in the animation list.
Linked lists are also overkill for this as they use extra memory (more things need to be stored)
setFrame(frames[++currFrame]);if(currFrame == maxFrames)  currFrame = 0;
but in contrast
curr_frame = *(curr_frame.next);// or if that's a pointercurr_frame = curr_frame.next;
The array will have better caching performance. It might be miniscule, but miniscule savings add up quickly. One drop at a time, the pot is filled.
Quote:Original post by moagstar
*** Source Snippet Removed ***


Agh. Don't go indexing the array before you've corrected for the loop-back condition.

currFrame = currFrame + 1 % frames.length(); // assuming std::vector
setFrame(frames[currFrame]);

Anyway. I'd go with the array (well, std::vector) simply because getting real (idiosyncratic) benefit out of the linked list would require a circular linked list, and the STL list just doesn't do that. (And rolling your own is not terribly much fun, especially the part about making sure all the links are right when you add or remove something.)
You could use stl::vector instead of the array. Presumably you'll know how many frames of animation you have, so you can reserve memory before filling the vector:

vector<Sprite> animation;
animation.reserve(numFrames);

//now populate frames

Vectors offer all the efficiency benefits of arrays (the STL *requires* vectors implement their underlying storage as an array) but also offer additional benefits such as automatic growth, helper methods and standard STL algorithms. Assuming you're using C++ of course.

For your own functions, I'd recommend 1 == success, -n = failure. That way, negative numbers can be used to signify different errors. You could even have different (positive) success codes if you required it.

HTH,
Kent

This topic is closed to new replies.

Advertisement