Use STL containers instead of C arrays

Home Forums C Programming Use STL containers instead of C arrays

This topic contains 0 replies, has 1 voice, and was last updated by  will 12 years, 10 months ago.

  • Author
    Posts
  • #1879

    will
    Member

    When using C++, you should always strive to use STL containers like std::vector and std::list instead of ordinary C arrays.

    Consider this example:

    All is fine, until doSomething() is changed to throw an exception. If doSomething throws, we will have a memory leak.

    The correct way of doing this is:

    If doSomething() throws an exception now, no memory will be leaked.

    This is just one of the reasons to use STL containers instead of C arrays. STL containers are much less error prone, more robust and easier to use.

You must be logged in to reply to this topic.

0 Shares
Share
+1
Tweet
Share
Stumble