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. C / C++ / MFC
  4. v. clear() ?

v. clear() ?

Scheduled Pinned Locked Moved C / C++ / MFC
5 Posts 4 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
    Yonggoo
    wrote on last edited by
    #1

    Hi all, I am not sure of the following code. vector::iterator it; for(it = vecGuiInSockets.begin(); it != vecGuiInSockets.end(); ++it) { (*it)->cleanup_socket(); delete (*it); it--; } vecGuiInSockets.clear(); First of all, what does the clear() clean? Does it just clean the pointers to Socket, or it cleans the object to which the pointer points? Second, is the for() loop correctly done? Thanks!

    Yonggoo

    T S 2 Replies Last reply
    0
    • Y Yonggoo

      Hi all, I am not sure of the following code. vector::iterator it; for(it = vecGuiInSockets.begin(); it != vecGuiInSockets.end(); ++it) { (*it)->cleanup_socket(); delete (*it); it--; } vecGuiInSockets.clear(); First of all, what does the clear() clean? Does it just clean the pointers to Socket, or it cleans the object to which the pointer points? Second, is the for() loop correctly done? Thanks!

      Yonggoo

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      since you didn't use &lt; and &gt; rather than < and > respectedly, i cannot know what your vactor contains. so if my answer is not accurate, please fix your question. the std::vector<>::clear() function clears the vector, but nowhere calls the objects destructors. if your vector contains objects (like std::vector<MyType>), then you manage the life of the objects. calling clear() will only remove the reference (actually, the copy) of the obect in the vector. if your vector contains pointers (like std::vector<MyType*>), you have to be more than careful because memory leak are close to occur; especially if you allocated dynamically using new. Using such vectors, you must first iterate over the vector to delete all the contained pointers, and only then call clear(). ps: please also use <pre></pre> tags when posting code sample so that you keep being formated correctly


      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      D 1 Reply Last reply
      0
      • T toxcct

        since you didn't use &lt; and &gt; rather than < and > respectedly, i cannot know what your vactor contains. so if my answer is not accurate, please fix your question. the std::vector<>::clear() function clears the vector, but nowhere calls the objects destructors. if your vector contains objects (like std::vector<MyType>), then you manage the life of the objects. calling clear() will only remove the reference (actually, the copy) of the obect in the vector. if your vector contains pointers (like std::vector<MyType*>), you have to be more than careful because memory leak are close to occur; especially if you allocated dynamically using new. Using such vectors, you must first iterate over the vector to delete all the contained pointers, and only then call clear(). ps: please also use <pre></pre> tags when posting code sample so that you keep being formated correctly


        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        toxcct wrote:

        so if my answer is not accurate, please fix your question.

        Best answer I've heard all day! :-D


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        T 1 Reply Last reply
        0
        • D David Crow

          toxcct wrote:

          so if my answer is not accurate, please fix your question.

          Best answer I've heard all day! :-D


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          DavidCrow wrote:

          Best answer I've heard all day!

          if my answer was that good, then some idiot didn't understood it, because it seems that i've been rated badly (once again) :(


          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          1 Reply Last reply
          0
          • Y Yonggoo

            Hi all, I am not sure of the following code. vector::iterator it; for(it = vecGuiInSockets.begin(); it != vecGuiInSockets.end(); ++it) { (*it)->cleanup_socket(); delete (*it); it--; } vecGuiInSockets.clear(); First of all, what does the clear() clean? Does it just clean the pointers to Socket, or it cleans the object to which the pointer points? Second, is the for() loop correctly done? Thanks!

            Yonggoo

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            I doubt the code will work. It doesn't help that we can see < or > characters in your post! The it-- is crazy and will cause all types of problems.

            Steve

            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