I build an array with help with this question: How do I declare a 2d array in C++ using new?
Now, my question is, how do I delete it?
I know it's a for loop and then other delete, but I need help with details.
            Asked
            
        
        
            Active
            
        
            Viewed 98 times
        
    -2
            
            
        - 
                    Hint: For every call to new, there should be a call to delete. Also, C++ have different operators for deleting arrays and deleting objects. – Etienne Maheu Nov 17 '14 at 14:42
- 
                    2Stop using dynamic arrays, and start using [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) instead. – Some programmer dude Nov 17 '14 at 14:43
- 
                    2Hint: never ever use the keyword `delete`. Look up RAII – learnvst Nov 17 '14 at 14:43
- 
                    1Have you looked at http://stackoverflow.com/a/936709/1433901 ? – MariusSiuram Nov 17 '14 at 14:47
- 
                    @MariusSiuram LOL!! Now I see, but here is much more clear!! From there I get confuse!! – Yoar Nov 17 '14 at 14:59
- 
                    @learnvst That's never use array new (and thus array delete). You need `delete` for handling objects with arbitrary lifetime. (And RAII has nothing to do with the issue. As a general rule, if RAII can be used, you shouldn't be allocating dynamically to begin with.) – James Kanze Nov 17 '14 at 14:59
1 Answers
3
            The same way you created it. If you have a 2D array, yourArray, that has rows number of rows.
for (int i = 0; i < rows; ++i)
{
    delete [] yourArray[i];
}
delete [] yourArray;
 
    
    
        Cory Kramer
        
- 114,268
- 16
- 167
- 218
 
    