I’m utilizing a library written by a collegue and discovered that valgrind was spewing out errors related to the delete.
The problem was that there were allocations of char arrays like
char* s = new char[n];
followed up later with delete s
instead of delete[] s
He tells me that the difference is really that delete[] s will call a destructor for the object at each position in s (if it has one), in this case it doesn’t because it’s a primitive type. I believe that is true.
So delete s is not really a bug as such and valgrind is just being very thorough.
Will it still definitely free all the memory associated with s?
If you allocate an array using
new[], you have to destroy it usingdelete[]. In general, the functionsoperator delete(void*)andoperator delete[](void*)aren’t guaranteed to be the same.Refer here