Archived

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

v71

stl

Recommended Posts

v71    100
Hi, can anyone tell me if its worth it to use stl instead of normal arrays or dynamic arrays done at ''hand'' ? is there a speed issue to consider ? thanx

Share this post


Link to post
Share on other sites
Shannon Barber    1681
Generally, the STL will produce (ever so slightly) tighter code than you or I will. The only speed issue to consider when using stl::vector is when you resize it, and is no different than using your own resizable array.

It''s worth learning how to use the STL for the proven containers it provides. You can be reasonably certain that the containers will work as excepted; there are (or were) a few bugs in the MS/HP version, but they are fairly minor and may have been corrected in a SP release. It''s easy to make a mistake when coding a simple linked list, and then you waste time debugging it; the STL saves you from this.

If you''re not used to working with templates, and are accustomed to the void* techniques, then the STL is even better. It provides type-safety without code-bloat for all the containers that are instanced - which AFAIK is not possible in any other language. If anyone knows of any others, please let me know.

The vectors can be bound checked in a debug build, and optimized out in a release build, so you get the best of all possible worlds. However, I don''t think the STL currently does this (at least not the MS/HP version).


Magmai Kai Holmlor
- Not For Rent

Share this post


Link to post
Share on other sites
NuffSaid    122
I haven''t used the STL enough to know if this is true, but I''ve heard that the STL makes lots of tiny memory allocations and causes memory fragmentation, resulting in poor app performance.

I got these from quite a few places, but the only one I can remember at the moment is the Post Mortem of Star Wars Starfighter on Gamasutra.

Share this post


Link to post
Share on other sites
Sneftel    1788
If you expect STL to be some magical toolkit with all the advantages of different data structures, and none of their disadvantages, memory fragmentation will surely result. If, however, you spend some time reading the specification and getting to know good allocation habits (just like you should for any dynamic data structures), STL can be just as efficient as your own code.

Share this post


Link to post
Share on other sites
NuffSaid    122
quote:
Original post by Magmai Kai Holmlor
Does the STL make the tiny allocations, or the programmer using it?



Good point. I''ll have to start experimenting and going through the specs.

Share this post


Link to post
Share on other sites
shaft    126
The STL really doesn''t make anymore memory allocations than if you had programmed a similar class yourself.

For example, if you allocate a vector with 10 spaces, and then insert an 11th. The code will work fine and you might think every things groovy. But behind the curtain, stl saw that enough memory wasn''t already allocated, allocates 11 spaces, and then copies all the values over to the new array. The original list of 10 is de-allocated, and results most likely in fragmented memory.

If however, you set the vectors initial Max Size to 50, and never go above 50, you should not created any tiny memory fragments.

STL can''t think for you, but it gives you ways that you can set it up so it works optimally.

I like stl.

Share this post


Link to post
Share on other sites
merlin9x9    174
The STL's implementations are as good or better than you or I could do ourselves. None of its classes make any unnecessary allocations. They were designed for efficiency, and they make certain guarantees (like vector's O(1) complexity for random access). If you want to prevent constant reallocation that results in memory fragmentation, do the same thing you'd do in your own code: reserve the space beforehand (which is what the the STL containers' reserve and resize functions are for).

Use the STL.

Edited by - merlin9x9 on August 3, 2001 1:10:21 PM

Share this post


Link to post
Share on other sites