Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Destructors in STL?

Destructors in STL?

Scheduled Pinned Locked Moved ATL / WTL / STL
questionc++
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sawerr
    wrote on last edited by
    #1

    Hi I see that all stl components have destructors. string s; s.~string(); But i know that we don't need to delete stl componenets.I read all stl components automatically delete themself.Is this wrong? If true what is the function of destructors in STL? Thanks...

    C S Z 3 Replies Last reply
    0
    • S sawerr

      Hi I see that all stl components have destructors. string s; s.~string(); But i know that we don't need to delete stl componenets.I read all stl components automatically delete themself.Is this wrong? If true what is the function of destructors in STL? Thanks...

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      How do you expect STL components to magically delete themselves ? Cleaning up after themselves is what C++ classes have destructors for.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      A 1 Reply Last reply
      0
      • S sawerr

        Hi I see that all stl components have destructors. string s; s.~string(); But i know that we don't need to delete stl componenets.I read all stl components automatically delete themself.Is this wrong? If true what is the function of destructors in STL? Thanks...

        S Offline
        S Offline
        sunit5
        wrote on last edited by
        #3

        say u have vector in such case the vector will delete the pointers but the memory referred by the pointers.So it leads to memory leak

        never say die

        G 1 Reply Last reply
        0
        • S sawerr

          Hi I see that all stl components have destructors. string s; s.~string(); But i know that we don't need to delete stl componenets.I read all stl components automatically delete themself.Is this wrong? If true what is the function of destructors in STL? Thanks...

          Z Offline
          Z Offline
          Zac Howland
          wrote on last edited by
          #4

          sawerr wrote:

          But i know that we don't need to delete stl componenets

          I don't think you quite understand C++ fundamentals. STL classes (string, vector, list, map, etc) all allocate memory on the heap to store whatever data you are throwing at them. string, for example, allocates an array of characters. As you fill up that array, it evaluates if it needs to allocate a bigger buffer, etc. All that is handled for you. The destructor cleans up that heap memory. When you write:

          void f()
          {
          	string s;
          }
          

          s is allocated on the stack. At the end of the scope (in this case, when the function returns), s's destructor is called, which cleans up anything s had allocated. If you were to write this:

          void f()
          {
          	string* s = new string;
          }
          

          you would have a memory leak (not good!).

          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

          1 Reply Last reply
          0
          • C Christian Graus

            How do you expect STL components to magically delete themselves ? Cleaning up after themselves is what C++ classes have destructors for.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            A Offline
            A Offline
            Anonymuos
            wrote on last edited by
            #5

            Christian Graus wrote:

            How do you expect STL components to magically delete themselves ?

            He probably read somwhere that he need not learn the basics but jump right into STL programming. :suss:

            1 Reply Last reply
            0
            • S sunit5

              say u have vector in such case the vector will delete the pointers but the memory referred by the pointers.So it leads to memory leak

              never say die

              G Offline
              G Offline
              George L Jackson
              wrote on last edited by
              #6

              If you have a vector of pointers to dynamically created objects, you must explicitly delete those objects before the vector goes out of scope: using std::wcout; using std::endl; using std::vector; class Foo { public: Foo(int index) : m_index(index) { wcout << L"Creating Foo #" << m_index << endl; } ~Foo() { wcout << L"Destroying Foo #" << m_index << endl; } private: int m_index; }; int _tmain(int argc, _TCHAR* argv[]) { vector pFooList; for (int i = 0; i < 10; ++i) { pFooList.push_back(new Foo(i)); } for (int i = 0; i < 10; ++i) { delete pFooList[i]; } return 0; } -- modified at 0:01 Saturday 12th August, 2006

              D 1 Reply Last reply
              0
              • G George L Jackson

                If you have a vector of pointers to dynamically created objects, you must explicitly delete those objects before the vector goes out of scope: using std::wcout; using std::endl; using std::vector; class Foo { public: Foo(int index) : m_index(index) { wcout << L"Creating Foo #" << m_index << endl; } ~Foo() { wcout << L"Destroying Foo #" << m_index << endl; } private: int m_index; }; int _tmain(int argc, _TCHAR* argv[]) { vector pFooList; for (int i = 0; i < 10; ++i) { pFooList.push_back(new Foo(i)); } for (int i = 0; i < 10; ++i) { delete pFooList[i]; } return 0; } -- modified at 0:01 Saturday 12th August, 2006

                D Offline
                D Offline
                dfields326
                wrote on last edited by
                #7

                Use auto_ptr<> will delete them for you #include using std::wcout; using std::endl; using std::vector; class Foo { public: Foo(int index) : m_index(index) { wcout << L"Creating Foo #" << m_index << endl; } ~Foo() { wcout << L"Destroying Foo #" << m_index << endl; } private: int m_index; }; int _tmain(int argc, _TCHAR* argv[]) { vector< std::auto_ptr< Foo* > > pFooList; for (int i = 0; i < 10; ++i) { pFooList.push_back( std::auto_ptr< Foo* >(new Foo(i))); } /* no need to delete, std::auto_ptr takes care if it for you. //see #include for (int i = 0; i < 10; ++i) { delete pFooList[i]; } */ return 0; } :cool:

                G Z 2 Replies Last reply
                0
                • D dfields326

                  Use auto_ptr<> will delete them for you #include using std::wcout; using std::endl; using std::vector; class Foo { public: Foo(int index) : m_index(index) { wcout << L"Creating Foo #" << m_index << endl; } ~Foo() { wcout << L"Destroying Foo #" << m_index << endl; } private: int m_index; }; int _tmain(int argc, _TCHAR* argv[]) { vector< std::auto_ptr< Foo* > > pFooList; for (int i = 0; i < 10; ++i) { pFooList.push_back( std::auto_ptr< Foo* >(new Foo(i))); } /* no need to delete, std::auto_ptr takes care if it for you. //see #include for (int i = 0; i < 10; ++i) { delete pFooList[i]; } */ return 0; } :cool:

                  G Offline
                  G Offline
                  George L Jackson
                  wrote on last edited by
                  #8

                  :omg: Unfortunately, that should not compile! According to the book, "The C++ Standard Library: A Tutorial and Reference" by Nicolai M. Josuttis, I quote: auto_ptrs don't meet the requirements for container elements. An auto_ptr does not meet one of the most fundamental requirements for elements of standard containers. That is, after a copy or an assignment of an auto_ptr, source and sink are not equivalent. In fact, when an auto_ptr is assigned or copied, the source auto_ptr gets modified because it transfers its value rather than copying it. So, you should not use an auto_ptr as an element of a standard container. ... However, boost::shared_ptr can be used with containers! See: http://www.boost.org/libs/smart_ptr/shared_ptr.htm[^] George -- modified at 17:52 Thursday 17th August, 2006

                  G 1 Reply Last reply
                  0
                  • G George L Jackson

                    :omg: Unfortunately, that should not compile! According to the book, "The C++ Standard Library: A Tutorial and Reference" by Nicolai M. Josuttis, I quote: auto_ptrs don't meet the requirements for container elements. An auto_ptr does not meet one of the most fundamental requirements for elements of standard containers. That is, after a copy or an assignment of an auto_ptr, source and sink are not equivalent. In fact, when an auto_ptr is assigned or copied, the source auto_ptr gets modified because it transfers its value rather than copying it. So, you should not use an auto_ptr as an element of a standard container. ... However, boost::shared_ptr can be used with containers! See: http://www.boost.org/libs/smart_ptr/shared_ptr.htm[^] George -- modified at 17:52 Thursday 17th August, 2006

                    G Offline
                    G Offline
                    George L Jackson
                    wrote on last edited by
                    #9

                    #include #include #include using std::wcout; using std::endl; using std::vector; using boost::shared_ptr; class Foo { public: Foo(int index) : m_index(index) { wcout << L"Creating Foo #" << m_index << endl; } ~Foo() { wcout << L"Destroying Foo #" << m_index << endl; } private: int m_index; }; int _tmain(int argc, _TCHAR* argv[]) { vector< shared_ptr > pFooList2; for (int i = 0; i < 10; ++i) { shared_ptr sptr(new Foo(i)); pFooList2.push_back(sptr); } return 0; }

                    1 Reply Last reply
                    0
                    • D dfields326

                      Use auto_ptr<> will delete them for you #include using std::wcout; using std::endl; using std::vector; class Foo { public: Foo(int index) : m_index(index) { wcout << L"Creating Foo #" << m_index << endl; } ~Foo() { wcout << L"Destroying Foo #" << m_index << endl; } private: int m_index; }; int _tmain(int argc, _TCHAR* argv[]) { vector< std::auto_ptr< Foo* > > pFooList; for (int i = 0; i < 10; ++i) { pFooList.push_back( std::auto_ptr< Foo* >(new Foo(i))); } /* no need to delete, std::auto_ptr takes care if it for you. //see #include for (int i = 0; i < 10; ++i) { delete pFooList[i]; } */ return 0; } :cool:

                      Z Offline
                      Z Offline
                      Zac Howland
                      wrote on last edited by
                      #10

                      dfields326 wrote:

                      Use auto_ptr<> will delete them for you

                      Containers of auto_ptr's is a BIG No-No! It will compile on some older compilers, but it will not do anything close to what you expect it to. It won't even compile on newer compilers.

                      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups