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
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Does STL vector automatically free content ?

Does STL vector automatically free content ?

Scheduled Pinned Locked Moved ATL / WTL / STL
c++comgraphicsquestion
5 Posts 3 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.
  • Y Offline
    Y Offline
    yarp
    wrote on last edited by
    #1

    I recently added STL vector to some 3D classes I have in order to store a huge amount of datas. I've read somewhere that vector doesn't free object untill it is freed. But I thought that myvector.clear() would free the content. I just experienced it doesn't: My classes are prototyped like this: class C3dObject { public: C3dObject(TCHAR* pszObjectName); ~C3dObject(); std::vector m_vctVertex; } In the destructor, I just call: m_vctVertex.clear(); and this frees nothing (The C3dVertex class has the same prototyping). Has someone an explanation to this ? Thanks Yarp http://www.senosoft.com/

    Y S N 3 Replies Last reply
    0
    • Y yarp

      I recently added STL vector to some 3D classes I have in order to store a huge amount of datas. I've read somewhere that vector doesn't free object untill it is freed. But I thought that myvector.clear() would free the content. I just experienced it doesn't: My classes are prototyped like this: class C3dObject { public: C3dObject(TCHAR* pszObjectName); ~C3dObject(); std::vector m_vctVertex; } In the destructor, I just call: m_vctVertex.clear(); and this frees nothing (The C3dVertex class has the same prototyping). Has someone an explanation to this ? Thanks Yarp http://www.senosoft.com/

      Y Offline
      Y Offline
      yarp
      wrote on last edited by
      #2

      oops, my vector class is declared as follows (think it's due to tag like syntax in the vector's declaration): std::vector<C3dVertex*> m_vctVertex; Yarp http://www.senosoft.com/

      1 Reply Last reply
      0
      • Y yarp

        I recently added STL vector to some 3D classes I have in order to store a huge amount of datas. I've read somewhere that vector doesn't free object untill it is freed. But I thought that myvector.clear() would free the content. I just experienced it doesn't: My classes are prototyped like this: class C3dObject { public: C3dObject(TCHAR* pszObjectName); ~C3dObject(); std::vector m_vctVertex; } In the destructor, I just call: m_vctVertex.clear(); and this frees nothing (The C3dVertex class has the same prototyping). Has someone an explanation to this ? Thanks Yarp http://www.senosoft.com/

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

        vector doesn't free anything because the things it's storing aren't objects. You're storing pointers. You'd need to modify it to be: for( it = m_vctVertex.begin(); it != m_vctVertex.end(); ++it) { if (*it) delete (*it); } m_vctVertex.clear(); where it is an iterator, as usual. If the thing was an object itself, eg CString or struct xxx or whatever, then it would do what you expect. Steve S

        1 Reply Last reply
        0
        • Y yarp

          I recently added STL vector to some 3D classes I have in order to store a huge amount of datas. I've read somewhere that vector doesn't free object untill it is freed. But I thought that myvector.clear() would free the content. I just experienced it doesn't: My classes are prototyped like this: class C3dObject { public: C3dObject(TCHAR* pszObjectName); ~C3dObject(); std::vector m_vctVertex; } In the destructor, I just call: m_vctVertex.clear(); and this frees nothing (The C3dVertex class has the same prototyping). Has someone an explanation to this ? Thanks Yarp http://www.senosoft.com/

          N Offline
          N Offline
          Nemanja Trifunovic
          wrote on last edited by
          #4

          Take a look at this article[^].

          Y 1 Reply Last reply
          0
          • N Nemanja Trifunovic

            Take a look at this article[^].

            Y Offline
            Y Offline
            yarp
            wrote on last edited by
            #5

            Thank you both for your explanation. I thought this had something to do with pointer based vector, I understand why now... and btw Nemanja your article is very interesting. STL obviously is a very powerfull concept. Yarp http://www.senosoft.com/

            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