Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gimp

Should I be using STL?

This topic is 6540 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, after spending a fair amount of time recoding part of my game recently I decided to submit it to a public MS VC++ newsgroup and see if any of the programmers there were interested in stepping through my code and letting me know what their thoughts are.... The overwealming reaction was... use stl. -I''m mallocing everything myself, mostly strings. I was recommended basic_string -I use some basic C code for reading file in to a memory map. I was recommended iostream -I have a bunch of linked lists and queue. I was recommended map and queue and vector? etc the list goes on... So, from a game programmers perspective, should I be doing this? I''m willing to accept a minor speed reduction so that I dont have to spend so much time debugging. I also need my code to be threadsafe, which isn''t difficult though all i know how to use is mutex''s. So, in the opinions of game programmers should I rewrite this code again to teach myself STL. (The STL code wouldn''t be used in any graphics programming, just core game code, message handling, loaders, networking etc) thanks gimp

Share this post


Link to post
Share on other sites
Advertisement
i would have to say yes (if you have the time and patience to do a re-write).

i try to use STL at every opportunity.

Share this post


Link to post
Share on other sites
STL is written with speed in mind, so it shouldn''t be much slower than anything you write yourself. But you have to use the correct type for the correct purpose.

list and vector look the same, but they''re implemented differently: list is a doubly linked list, vector is an array. So if you''re going to do a lot of insertions in the middle, list is faster, but if you''re going to do a lot of random access, vector is faster.

I''d say go with STL, because the code is probably more bug free than anything you write yourself to do the same.

Erik

Share this post


Link to post
Share on other sites
The STL is C++ code, so is iostream
malloc isn''t!

DO NOT MIX malloc''s with new''s!
& demalloc with deletes

It sounds as though you created a C program, and everyone gave you C++ advice...

Share this post


Link to post
Share on other sites
Hey!
I once wrote a linked list, and then I heard of the STL. Being rather naive then, I thought that _my_ list was goning to be faster, since it is more specialized. So, using a profiler, what did I find?
Yes, you guessed it, the STL was faster, tho not by much (how many ways can you do a linked list? ). So at that point I decided to use the STL. Me being used to C++ classes etc, the STL uses C++ stuff but it doesn''t use classes in the normal way. You see what I mean if you look at the reference (generic algorithisms (<- spelt wrong?) etc). Also using the STL with pointers is a pain in the ass.
So I did a load of wrappers around the STL, that make it simpler, but not slower - all the functions are inline and it uses templates like the STL. In fact, I am pretty proud of it - it hasn''t had one bug yet. I would make a wrapper around it, so that its simpler to use -
std::list ASurface becomes:
Tools::List > (Safe pointer automatically deals with memory management. I don''t have any memory leaks - OK not that many ). For instance, using the STL to iterate through anything while deleting pointers in that list is a pain in the ass.It took ages to learn the STL (at least for me. Maybe I''m just dumb . Oh yeah, if you have the code to the STL (i think everyone has it) you''ll see that much of the code is in assembly... pretty fast.

Happy (or not so happy) coding with the STL.

Share this post


Link to post
Share on other sites
Thanks guy''s looks like I''ll get stuck in to it then, I''ve got MSDN installed which has some pretty lame doco on STL (IMHO). Is there anything around that might be better for me? Something with examples perhaps?

Thanks

Chris

Share this post


Link to post
Share on other sites
Ahhhh.... many thanks.. I''ve been searching MSDN for a while with no example to be found... I guess they prefer MFC

Share this post


Link to post
Share on other sites
Hrrmmm.... That SGI site doesn''t refer to iostream... I guess that must outside their scope.

Could anyone offer a little code snippet that will open a text file and read it in to memory and get a few character from the data?

Or perhaps point me in the direction of some examples? I''m looking through MSDN''s doco and it has too many options to count and I''m not sure where to start....

Many thanks

Chris

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!