delete an array

I have a struct like this:

1
2
3
4
5
    class ClassOps{
        
        public:
        Array *array2[100];
        };


Say i want to delete the whole array


1
2
3
4
       for ( int g=0; g<5; g++){
        delete [] array2;
        array2= NULL;
        }


Is there a way to remove an array entirely? Seems to have problem with that
Of course it has a problem with that. You may only delete what has been new'd. Is there some reason you though you needed to try the delete 5 times?

Is there a way to remove an array entirely?


Sure. Allow the ClassOps object to expire.
You can not delete the array because you did not allocate it in the heap. You can delete elements of the array of type Array * if they were allocated in the heap. It can be done for example the following way

for ( int i = 0; i < 100; i++ ) delete array2[i];
Last edited on
You can not delete the array because you did not allocate it in the heap. You can delete elements of the array of type Array * if they were allocated in the heap. It can be done for example the following way

for ( int i = 0; i < 100; i++ ) delete array2[i];


If i follow with this code, the application will hang....


Of course it has a problem with that. You may only delete what has been new'd. Is there some reason you though you needed to try the delete 5 times?

Is there a way to remove an array entirely?


Sure. Allow the ClassOps object to expire.


How to i set the expire?
You let it go out of scope.
Topic archived. No new replies allowed.