Consider this example:
int* array = new array[size];
doSomething(array);
delete[] array;
All is fine, until doSomething() is changed to throw an exception. If doSomething throws, we will have a memory leak.
The correct way of doing this is:
vectorarray(size);
doSomething();
If doSomething() throws an exception now, no memory will be leaked.
This is just one of the reasons to use STL containers instead of C arrays. STL containers are much less error prone, more robust and easier to use.
Author : Sang-drax
0 nhận xét:
Post a Comment