so, giving examples of how use dynamically-allocated 2d arrays , send code following:
int size = 5; int* arr = new int[ size ]; for( int = 0; < size; i++ ) arr[ ] = i; delete[] arr; size = 10; int* arr = new int[ size ]; for( int = size; > 0; i-- ) arr[ ] = i; delete[] arr;
this gave me redefinition error, thought delete[] frees space in memory (and 'arr'). know how work around (new array name, don't delete[]/redefine), wondering what's going on gives error?
you might want try this:
int size = 5; int* arr = new int[ size ]; for( int = 0; < size; i++ ) arr[ ] = i; delete[] arr; size = 10; arr = new int[ size ]; //<-- no int* here, need reassign for( int = size; > 0; i-- ) arr[ ] = i; delete[] arr;
we indeed deallocating block of memory arr points to, doesn't mean removing int* arr. removed it's 'content'.
it's non assigned pointer again after delete it.
Comments
Post a Comment