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