deleting array of objects of a class
-
Hi to all, I am have written some classes in C++ as follows, Class A { int b1Len; //count of B type objects in b1Collection B *b1Collection; //stores collection of object of type B int b2Len; //count of B type objects in b2Collection B *b2Collection; //stores collection of object of type B }; Class B { int cLen; //count of C type objects in cCollection //cLen is atleast 30 C *cCollection; //stores collection of object of type C }; Class C { int d1Len; //count of D type objects in d1Collection //d1Len is atleast 140 D *d1Collection; //stores collection of object of type D int d2Len; //count of D type objects in d2Collection D *d2Collection; //stores collection of object of type D int e1Len; //count of E type objects in e1Collection E *e1Collection; //stores collection of object of type E int e2Len; //count of E type objects in e2Collection E *e2Collection; //stores collection of object of type E }; Class D { int fLen; //count of F type objects in fCollection //fLen is atleast 2 F *fCollection; //stores collection of object of type F }; Class F { int len; //count of characters in str //len is atleast 200 char *str; //stores multiple characters }; int aLen; //count of A type objects in aCollection A *aCollection; //stores collection of object of type A DelAll(aCollection, aLen); //aLen is atleast 30 //it's a macro to delete array of objects of any type #define DelAll(obj, no) if( obj != NULL ) { if( 0 < no ) { delete[] obj; obj = NULL; } } I am using this macro in destructor of all class (above) to delete array of objects. My problem is, it's takes atleast 0.86 secs Is there anything wrong I am doing? I am using VC++ 2008 as IDE. Thanks & Regards, Aniket A. Salunkhe
-
Hi to all, I am have written some classes in C++ as follows, Class A { int b1Len; //count of B type objects in b1Collection B *b1Collection; //stores collection of object of type B int b2Len; //count of B type objects in b2Collection B *b2Collection; //stores collection of object of type B }; Class B { int cLen; //count of C type objects in cCollection //cLen is atleast 30 C *cCollection; //stores collection of object of type C }; Class C { int d1Len; //count of D type objects in d1Collection //d1Len is atleast 140 D *d1Collection; //stores collection of object of type D int d2Len; //count of D type objects in d2Collection D *d2Collection; //stores collection of object of type D int e1Len; //count of E type objects in e1Collection E *e1Collection; //stores collection of object of type E int e2Len; //count of E type objects in e2Collection E *e2Collection; //stores collection of object of type E }; Class D { int fLen; //count of F type objects in fCollection //fLen is atleast 2 F *fCollection; //stores collection of object of type F }; Class F { int len; //count of characters in str //len is atleast 200 char *str; //stores multiple characters }; int aLen; //count of A type objects in aCollection A *aCollection; //stores collection of object of type A DelAll(aCollection, aLen); //aLen is atleast 30 //it's a macro to delete array of objects of any type #define DelAll(obj, no) if( obj != NULL ) { if( 0 < no ) { delete[] obj; obj = NULL; } } I am using this macro in destructor of all class (above) to delete array of objects. My problem is, it's takes atleast 0.86 secs Is there anything wrong I am doing? I am using VC++ 2008 as IDE. Thanks & Regards, Aniket A. Salunkhe
It'd be helpful if you post a working code. The above class structure and objects wont take too much time. Please carefully check the destructors of the classes. You can roughly calculate the time using OutputDebugString API. Just start Debug View Tool and set the time to display in Milliseconds. then execute the code after putting enough log. or use some profiling or logging mechanisms to get the critical point.
-Sarath. "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts
-
Hi to all, I am have written some classes in C++ as follows, Class A { int b1Len; //count of B type objects in b1Collection B *b1Collection; //stores collection of object of type B int b2Len; //count of B type objects in b2Collection B *b2Collection; //stores collection of object of type B }; Class B { int cLen; //count of C type objects in cCollection //cLen is atleast 30 C *cCollection; //stores collection of object of type C }; Class C { int d1Len; //count of D type objects in d1Collection //d1Len is atleast 140 D *d1Collection; //stores collection of object of type D int d2Len; //count of D type objects in d2Collection D *d2Collection; //stores collection of object of type D int e1Len; //count of E type objects in e1Collection E *e1Collection; //stores collection of object of type E int e2Len; //count of E type objects in e2Collection E *e2Collection; //stores collection of object of type E }; Class D { int fLen; //count of F type objects in fCollection //fLen is atleast 2 F *fCollection; //stores collection of object of type F }; Class F { int len; //count of characters in str //len is atleast 200 char *str; //stores multiple characters }; int aLen; //count of A type objects in aCollection A *aCollection; //stores collection of object of type A DelAll(aCollection, aLen); //aLen is atleast 30 //it's a macro to delete array of objects of any type #define DelAll(obj, no) if( obj != NULL ) { if( 0 < no ) { delete[] obj; obj = NULL; } } I am using this macro in destructor of all class (above) to delete array of objects. My problem is, it's takes atleast 0.86 secs Is there anything wrong I am doing? I am using VC++ 2008 as IDE. Thanks & Regards, Aniket A. Salunkhe
(not really an answer) Why are you using C type "collections" instead of std::vector (or std::list) ? Are you checking the time in DEBUG or in RELEASE ? I don't see much wrong in what you presented if the destructors are really deleting what has been allocated; you probably have more data than you are expecting.
-
Hi to all, I am have written some classes in C++ as follows, Class A { int b1Len; //count of B type objects in b1Collection B *b1Collection; //stores collection of object of type B int b2Len; //count of B type objects in b2Collection B *b2Collection; //stores collection of object of type B }; Class B { int cLen; //count of C type objects in cCollection //cLen is atleast 30 C *cCollection; //stores collection of object of type C }; Class C { int d1Len; //count of D type objects in d1Collection //d1Len is atleast 140 D *d1Collection; //stores collection of object of type D int d2Len; //count of D type objects in d2Collection D *d2Collection; //stores collection of object of type D int e1Len; //count of E type objects in e1Collection E *e1Collection; //stores collection of object of type E int e2Len; //count of E type objects in e2Collection E *e2Collection; //stores collection of object of type E }; Class D { int fLen; //count of F type objects in fCollection //fLen is atleast 2 F *fCollection; //stores collection of object of type F }; Class F { int len; //count of characters in str //len is atleast 200 char *str; //stores multiple characters }; int aLen; //count of A type objects in aCollection A *aCollection; //stores collection of object of type A DelAll(aCollection, aLen); //aLen is atleast 30 //it's a macro to delete array of objects of any type #define DelAll(obj, no) if( obj != NULL ) { if( 0 < no ) { delete[] obj; obj = NULL; } } I am using this macro in destructor of all class (above) to delete array of objects. My problem is, it's takes atleast 0.86 secs Is there anything wrong I am doing? I am using VC++ 2008 as IDE. Thanks & Regards, Aniket A. Salunkhe
It looks like you have many arrays of objects, and a destructor is called for EACH array element. It may be possible to delete the whole array with one statement, if your destructor is just freeing memory (as opposed to executing cleanup code). For example: D* dArray = (D *) malloc (numberOfDObjects * sizeof (D)); ... delete [] (char *) dArray; This invokes delete once, rather than numberOfDObjects times. You may be able to use similar logic for arrays of arrays of objects.
-
It looks like you have many arrays of objects, and a destructor is called for EACH array element. It may be possible to delete the whole array with one statement, if your destructor is just freeing memory (as opposed to executing cleanup code). For example: D* dArray = (D *) malloc (numberOfDObjects * sizeof (D)); ... delete [] (char *) dArray; This invokes delete once, rather than numberOfDObjects times. You may be able to use similar logic for arrays of arrays of objects.
malloc
anddelete
do not play well together. did you meant to usefree
instead of delete ? -
Hi to all, I am have written some classes in C++ as follows, Class A { int b1Len; //count of B type objects in b1Collection B *b1Collection; //stores collection of object of type B int b2Len; //count of B type objects in b2Collection B *b2Collection; //stores collection of object of type B }; Class B { int cLen; //count of C type objects in cCollection //cLen is atleast 30 C *cCollection; //stores collection of object of type C }; Class C { int d1Len; //count of D type objects in d1Collection //d1Len is atleast 140 D *d1Collection; //stores collection of object of type D int d2Len; //count of D type objects in d2Collection D *d2Collection; //stores collection of object of type D int e1Len; //count of E type objects in e1Collection E *e1Collection; //stores collection of object of type E int e2Len; //count of E type objects in e2Collection E *e2Collection; //stores collection of object of type E }; Class D { int fLen; //count of F type objects in fCollection //fLen is atleast 2 F *fCollection; //stores collection of object of type F }; Class F { int len; //count of characters in str //len is atleast 200 char *str; //stores multiple characters }; int aLen; //count of A type objects in aCollection A *aCollection; //stores collection of object of type A DelAll(aCollection, aLen); //aLen is atleast 30 //it's a macro to delete array of objects of any type #define DelAll(obj, no) if( obj != NULL ) { if( 0 < no ) { delete[] obj; obj = NULL; } } I am using this macro in destructor of all class (above) to delete array of objects. My problem is, it's takes atleast 0.86 secs Is there anything wrong I am doing? I am using VC++ 2008 as IDE. Thanks & Regards, Aniket A. Salunkhe
Is it surrounded by a bunch of try ... catch blocks? it shouldn't take is much time though. Even in debug mode.
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]
-
malloc
anddelete
do not play well together. did you meant to usefree
instead of delete ?Oops, my mistrake. You can use free ((char *) dArray), or D* dArray = (D *) new char [numberOfDObjects * sizeof (D)]; and delete [] (char *) dArray
-
It'd be helpful if you post a working code. The above class structure and objects wont take too much time. Please carefully check the destructors of the classes. You can roughly calculate the time using OutputDebugString API. Just start Debug View Tool and set the time to display in Milliseconds. then execute the code after putting enough log. or use some profiling or logging mechanisms to get the critical point.
-Sarath. "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts
Hi to all, Thanks for your suggestions. In every class I was using Add___() function to add object of other class. I was reading a 35 MB xml & using that I was calling Add___() every time a found an object of class A, B, C, D, .. & I was getting problem at DelAll macro (it was taking more time to execute that might be due to lot of data). Now I have replaced the Add___() function call, by it’s function body. Now it is taking normal time to execute DelAll macro
class A { void Add_B(const B* newBs, int newBsCount) { if( newBsCount <= 0 ) return; B* temp_Bs = NULL; if( 0 < AllBsCount ) temp_Bs = new B[AllBsCount ]; int i for( i= 0 ; i < AllBsCount; i++ ) temp_ Bs [i] = AllDays[i]; **DelAll(AllBs, AllBsCount)** //problem macro AllBs = new B[AllBsCount + newBsCount]; for( i = 0 ; i < AllBsCount; i++ ) AllBs [i] = temp_Days[i]; for( i = 0 ; i < newBsCount; i++ ) AllBs [AllBsCount +i] = newBs [i]; DelAll(temp_ Bs, AllBsCount); AllBsCount += newBsCount; } }
Thanks & Best Regards, Aniket A. Salunkhe