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. RemoveAll() method

RemoveAll() method

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehelp
8 Posts 5 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.
  • K Offline
    K Offline
    Kleser
    wrote on last edited by
    #1

    Hi all, maybe somebody can help me out with this. if i do this: class CSomeClass { ...... }; CArray objSomeClassArray; CSomeClass objSomeClass; objSomeClassArray.Add(objSomeClass); ... objSomeClassArray.RemoveAll(); my question is: does the RemoveAll() method in this case free also the memory? if not what should i do to free them? thanks a lot

    G O J T 4 Replies Last reply
    0
    • K Kleser

      Hi all, maybe somebody can help me out with this. if i do this: class CSomeClass { ...... }; CArray objSomeClassArray; CSomeClass objSomeClass; objSomeClassArray.Add(objSomeClass); ... objSomeClassArray.RemoveAll(); my question is: does the RemoveAll() method in this case free also the memory? if not what should i do to free them? thanks a lot

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

      In your example, I believe memory is naturally freed. However, if you created the ObjSomeClass via the heap (using new), you would have to manually free the memory via delete.

      1 Reply Last reply
      0
      • K Kleser

        Hi all, maybe somebody can help me out with this. if i do this: class CSomeClass { ...... }; CArray objSomeClassArray; CSomeClass objSomeClass; objSomeClassArray.Add(objSomeClass); ... objSomeClassArray.RemoveAll(); my question is: does the RemoveAll() method in this case free also the memory? if not what should i do to free them? thanks a lot

        O Offline
        O Offline
        Owner drawn
        wrote on last edited by
        #3

        This is what MSDN says about it: Notice the difference between deleting an element's object and removing the element itself. Removing an element from the list merely removes the list's reference to the object. The object still exists in memory. When you delete an object, it ceases to exist and its memory is reclaimed. Thus, it is important to remove an element immediately after the element's object has been deleted so that the list won't try to access objects that no longer exist.

        CArray<CPerson*, CPerson*> myArray;

        int i = 0;
        while (i < myArray.GetSize() )
        {
        delete myArray.GetAt( i++ );
        }

        myArray.RemoveAll();


        Owner drawn Jesus Loves

        1 Reply Last reply
        0
        • K Kleser

          Hi all, maybe somebody can help me out with this. if i do this: class CSomeClass { ...... }; CArray objSomeClassArray; CSomeClass objSomeClass; objSomeClassArray.Add(objSomeClass); ... objSomeClassArray.RemoveAll(); my question is: does the RemoveAll() method in this case free also the memory? if not what should i do to free them? thanks a lot

          J Offline
          J Offline
          John R Shaw
          wrote on last edited by
          #4

          Objects are usualy responsible for freeing the memory they use. If your CArray is storing objects, then they will free the memory when they are removed. But if the CArray is only storing pointers to objects, then you are responsible for freeing the memory [associated with each object]. The reason for this is that the array object does not actualy know whether it is storing an object or a pointer to an object. INTP Every thing is relative...

          1 Reply Last reply
          0
          • K Kleser

            Hi all, maybe somebody can help me out with this. if i do this: class CSomeClass { ...... }; CArray objSomeClassArray; CSomeClass objSomeClass; objSomeClassArray.Add(objSomeClass); ... objSomeClassArray.RemoveAll(); my question is: does the RemoveAll() method in this case free also the memory? if not what should i do to free them? thanks a lot

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            consider the small situation:-

            funcFCC()
            {
            ClassA a;
            }

            here the object is destroyed when the function scope end....

            funcFCC()
            {
            ClassA *a=new ClassA;
            }

            here the Memory is not freed when scope of function end.. resulted in memory leak... same apply for the RemoveAll function.. hope you understand this! :)

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            cheers, Alok Gupta VC Forum Q&A :- I/ IV

            K 1 Reply Last reply
            0
            • T ThatsAlok

              consider the small situation:-

              funcFCC()
              {
              ClassA a;
              }

              here the object is destroyed when the function scope end....

              funcFCC()
              {
              ClassA *a=new ClassA;
              }

              here the Memory is not freed when scope of function end.. resulted in memory leak... same apply for the RemoveAll function.. hope you understand this! :)

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

              cheers, Alok Gupta VC Forum Q&A :- I/ IV

              K Offline
              K Offline
              Kleser
              wrote on last edited by
              #6

              thanks for all the responses, please look at this code: void COtherClass::FirstFunction(CArray<CSomeClass, CSomeClass&> & pSomeClassArray) { CSomeClass objSomeClass; pSomeClassArra.Add(objSomeClass); ...... } void COtherClass::SecondFunction() { // m_objSomeClassArray is a member variable array of COtherClass (Type CSomeClass) FirstFunction(&m_objSomeClassArray); ...... m_objSomeClassArray.RemoveAll(); } is this OK?

              T 1 Reply Last reply
              0
              • K Kleser

                thanks for all the responses, please look at this code: void COtherClass::FirstFunction(CArray<CSomeClass, CSomeClass&> & pSomeClassArray) { CSomeClass objSomeClass; pSomeClassArra.Add(objSomeClass); ...... } void COtherClass::SecondFunction() { // m_objSomeClassArray is a member variable array of COtherClass (Type CSomeClass) FirstFunction(&m_objSomeClassArray); ...... m_objSomeClassArray.RemoveAll(); } is this OK?

                T Offline
                T Offline
                ThatsAlok
                wrote on last edited by
                #7

                Kleser wrote:

                void COtherClass::FirstFunction(CArray<CSomeClass, CSomeClass&> & pSomeClassArray)
                {
                CSomeClass objSomeClass;
                pSomeClassArra.Add(objSomeClass);

                ......
                }

                void COtherClass::SecondFunction()
                {
                // m_objSomeClassArray is a member variable array of COtherClass (Type CSomeClass)

                FirstFunction(&m_objSomeClassArray);

                ......

                m_objSomeClassArray.RemoveAll();

                }

                Is CSomeClass implement the copy constructor?

                "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                cheers, Alok Gupta VC Forum Q&A :- I/ IV

                K 1 Reply Last reply
                0
                • T ThatsAlok

                  Kleser wrote:

                  void COtherClass::FirstFunction(CArray<CSomeClass, CSomeClass&> & pSomeClassArray)
                  {
                  CSomeClass objSomeClass;
                  pSomeClassArra.Add(objSomeClass);

                  ......
                  }

                  void COtherClass::SecondFunction()
                  {
                  // m_objSomeClassArray is a member variable array of COtherClass (Type CSomeClass)

                  FirstFunction(&m_objSomeClassArray);

                  ......

                  m_objSomeClassArray.RemoveAll();

                  }

                  Is CSomeClass implement the copy constructor?

                  "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                  cheers, Alok Gupta VC Forum Q&A :- I/ IV

                  K Offline
                  K Offline
                  Kleser
                  wrote on last edited by
                  #8

                  i made a mistake writing the code above. it has to look like this: void COtherClass::FirstFunction(CArray<CSomeClass, CSomeClass&>* pSomeClassArray) { .... } instead of this: void COtherClass::FirstFunction(CArray<CSomeClass, CSomeClass&>& pSomeClassArray) { .... } the method has a pointer parameter instead of a reference parameter. i guess CSomeClass does not implement a copy constructor, i have not added anything in the constructor neither write a new constructor in that class. :confused: i use the class the way it is as i show you before, and it seems to work. but i want to know how to implement a copy constructor, can you please show me how to do it? thanks in advance :-D

                  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