I recently discovered that a lot places in our code were doing something like this:
int * int_array = new int[1000];
// ... do things with int_array
delete int_array;
The problem of course is that it should be using the delete [] operator, not the regular delete operator.
The mystery is: This code has been working for literally years, when compiled by Visual Studio 2003 and 2005 on Windows, and GCC / clang on OS X. Why hasn't this caused things to go terribly wrong before now?
As I understand it, we're telling the compiler to deallocate memory the "wrong" way, and usually if you do that, something terrible happens and your program crashes. Why isn't this happening for us? Do modern compilers automatically "do the right thing" for you, or enough of the right thing that it doesn't matter for basic types, or something else? I can't accept that we simply got lucky, as this code has been in use for years, by thousands of customers under multiple different operating systems.
Note that I'm NOT asking for an excuse to do things wrong, just trying to understand why we aren't in trouble for doing things wrong. :)