Not Using STL is good because...

Discussion in 'General Discussion' started by imaxx, Aug 14, 2012.

  1. imaxx

    Newcomer

    Joined:
    Mar 9, 2012
    Messages:
    131
    Likes Received:
    1
    Location:
    cracks
    Today I did run in a funny issue in my work (let's say an engineering team in a cool company in a cool project).

    I won't enter details, but it all ended up to this, said by a 50yr SW engineer:


    ...just wanted to share with you all, it would have made my day if it weren't happening to me... :p
     
  2. Davros

    Legend

    Joined:
    Jun 7, 2004
    Messages:
    14,899
    Likes Received:
    2,312
    So we can rule microsoft out
     
  3. PixResearch

    Regular

    Joined:
    May 20, 2010
    Messages:
    187
    Likes Received:
    46
    Location:
    London, UK
    Warning treated as error: STL has been deprecated.
    Please use STL_s
     
  4. Humus

    Humus Crazy coder
    Veteran

    Joined:
    Feb 6, 2002
    Messages:
    3,217
    Likes Received:
    77
    Location:
    Stockholm, Sweden
    For the record, STL has portability issues. Some stuff behaves different on different platforms. Not necessarily in a way that is out of spec, but implementations vary and certainly performance characteristics. A typical example is calling std::vector.clear(). Some implementations will free the memory, some will merely set the size to zero retaining all allocated memory. To really make sure it frees the memory you have to write some really ugly code, e.g.: http://stackoverflow.com/questions/3172699/is-this-normal-behavior-for-a-stdvector

    If you have your own code, you have full control. You can free or retain memory as you please. You have a choice in exact algorithms and memory allocation schemes. Depending on the needs of your cool project in the cool company, this may matter quite a great deal. Certainly for game development these factors typically outweighs any perceived benefit of STL. For us, the use of STL is generally discouraged within the company. Of course, there are bits and pieces in there that makes sense for us too, like std::sort(), but using for instance an std::vector is rarely the right thing to do. Our own Array class on the other hand will behave exactly as we implemented it on all platforms.
     
  5. McHuj

    Veteran Regular Subscriber

    Joined:
    Jul 1, 2005
    Messages:
    1,434
    Likes Received:
    554
    Location:
    Texas
    I agree with him.
     
  6. ERP

    ERP Moderator
    Moderator Veteran

    Joined:
    Feb 11, 2002
    Messages:
    3,669
    Likes Received:
    49
    Location:
    Redmond, WA
    We avoid it, and I know a lot of places where using STL is against the coding standards.

    If you don't care about the implementation it's fine for what it is, IME the STL implementation is rarely what you actually want.
     
  7. imaxx

    Newcomer

    Joined:
    Mar 9, 2012
    Messages:
    131
    Likes Received:
    1
    Location:
    cracks
    This is really interesting - can you tell me why? A good reason to use your own list/tree instead of ready-made and tested ones.

    As long as you don't have issues with:
    * best possible speed, to death like i.e. vgame development.
    * embedded devices (where even a full clib would be a problem).

    @Humus: agree, for example ANSI doesnt say anything about i.e. going before begin iterator. Yet, what makes you think your code is better tested and stabler than STL one?
     
    #7 imaxx, Aug 15, 2012
    Last edited by a moderator: Aug 15, 2012
  8. McHuj

    Veteran Regular Subscriber

    Joined:
    Jul 1, 2005
    Messages:
    1,434
    Likes Received:
    554
    Location:
    Texas
    Funny you say that, because the two issues you mentioned are my issues.

    The main reasons are speed in embedded applications and full control over the code/implementation. Now, I'm not coming from the video game world, but a DSP one where performance to handcounted cycles is king. But even then, some times our code needs to execute on a non native platform like x86 where realtime speed isn,t an issue, but it would would be, imo, to cumbersome to maintain two implementations of code (one STL and one not) to enable that.
     
  9. 3dcgi

    Veteran Subscriber

    Joined:
    Feb 7, 2002
    Messages:
    2,436
    Likes Received:
    264
    A lot of people seem to not like STL, but Humus is the first I've casually run across that gave a specific reason. Most people just say it's too slow. That might be true for some cases and there are a lot of people here with more programming experience than I, but there are many applications that won't have a performance issue with STL and will benefit more from increased productivity.

    I use STL extensively in a database application that runs on Windows and Linux and while it's possible memory isn't being handled the same way I've never noticed any functional difference in my program.
     
  10. epicstruggle

    epicstruggle Passenger on Serenity
    Veteran

    Joined:
    Jul 24, 2002
    Messages:
    1,903
    Likes Received:
    45
    Location:
    Object in Space
    Don't use STL either, but that's mainly because I work in a assembly-like language where we dont have it.
     
  11. Ethatron

    Regular Subscriber

    Joined:
    Jan 24, 2010
    Messages:
    859
    Likes Received:
    262
    Writing your own allocator should overwrite black-box allocator behaviours, I think.
     
  12. itsmydamnation

    Veteran Regular

    Joined:
    Apr 29, 2007
    Messages:
    1,298
    Likes Received:
    396
    Location:
    Australia
    so then..... what should someone learning c++ do, everything i have touched pretty much uses STL whether its string or console in/out etc. As a hack do i just continue using STL or at what point is it worth building your own library of functions/data types etc.


    cheers
     
  13. Rodéric

    Rodéric a.k.a. Ingenu
    Moderator Veteran

    Joined:
    Feb 6, 2002
    Messages:
    3,986
    Likes Received:
    847
    Location:
    Planet Earth.
    I used stdint.h from C99 for types, and std::string (lazy :p) + std::vector (in tools).

    Ill defined spec, not playing well with serialization are two reasons to avoid STL.
    (Barely readable errors don't really help either.)

    A lot of programmers like short readable code, STL just doesn't fit.
     
  14. McHuj

    Veteran Regular Subscriber

    Joined:
    Jul 1, 2005
    Messages:
    1,434
    Likes Received:
    554
    Location:
    Texas
    I think you should learn STL. It's important to know. But it's kind of a judgment call when to use STL or not that just comes with experience.
     
  15. epicstruggle

    epicstruggle Passenger on Serenity
    Veteran

    Joined:
    Jul 24, 2002
    Messages:
    1,903
    Likes Received:
    45
    Location:
    Object in Space
    STL can make you lazy, I like to get my hands dirty and code most of what I could get for free with STL. Comes with risks, but you get to understand how things like memory allocation happen and where pitfalls can arise.
     
  16. Gubbi

    Veteran

    Joined:
    Feb 8, 2002
    Messages:
    3,528
    Likes Received:
    862
    IMO, the STL container templates has great utility. Probably not fit for hardcore performance kernels.

    Cheers
     
  17. AlexV

    AlexV Heteroscedasticitate
    Moderator Veteran

    Joined:
    Mar 15, 2005
    Messages:
    2,528
    Likes Received:
    107
    Focus on learning it and on those who teach it. Then you can move on to the grey area of quirky worries. There is a reason for which most books trying to teach usable modern C++ use the STL quite a bit, and it's not because the guys writing them are evil.
     
  18. Ethatron

    Regular Subscriber

    Joined:
    Jan 24, 2010
    Messages:
    859
    Likes Received:
    262
    Right. And you can program rather quick complex things, which is good when you come from high level languages and don't want to be bothered to implement every quinch of a simple common data-structure. If you need to start to be worried about specific aspects of a STL implementation (performance, compliance, etc.), you have become a pro anway. :) So learn it, use it, and judge it by it's utility value in time.
     
  19. Humus

    Humus Crazy coder
    Veteran

    Joined:
    Feb 6, 2002
    Messages:
    3,217
    Likes Received:
    77
    Location:
    Stockholm, Sweden
    Default allocators are typically just wrappers around new and delete anyway. But even with a custom allocator, you'd still have the problem I mentioned with clear(), because an implementation that retains the memory wouldn't even call into your allocator. If you're a game developer, the std::vector clear() issue can matter because Microsoft's and Sony's implementations do this differently. I don't remember which one did what, but this was an issue we ran into at one point.

    Another problem with std::vector is of course that every push to it will check capacity and potentially resize. Normally you know ahead of time how many items you will need, so for performance reasons it's better to just set capacity upfront and then fill it in. This pulls capacity checks and allocations out of the inner loop. Also reduces memory fragmentation.
     
  20. Npl

    Npl
    Veteran

    Joined:
    Dec 19, 2004
    Messages:
    1,905
    Likes Received:
    6
    But aint this something you can easily replace at any time? I think this is STLs biggest strength, once you seen through the design choices, you can take your own vector class or template and use it everywhere.
    you arent tied to specific classes.

    STL is a great thing to get going, the rest is something you can solve when its necessary
     
Loading...

Share This Page

  • About Us

    Beyond3D has been around for over a decade and prides itself on being the best place on the web for in-depth, technically-driven discussion and analysis of 3D graphics hardware. If you love pixels and transistors, you've come to the right place!

    Beyond3D is proudly published by GPU Tools Ltd.
Loading...