delete[] supplied a modified new-ed pointer. Undefined Behaviour?
问题 I saw some code as below during a peer-code-review session: char *s = new char[3]; *s++ = 'a'; *s++ = 'b'; *s++='\0'; delete []s; // this may or may not crash on some or any day !! Firstly, I know that in Standard C++, pointing to one-past the array-size is O.K. though accessing it results in undefined behaviour. So I believe the last line *s++='\0' is fine. But if I recall correctly, the C++ standard mandates that delete should be supplied the same pointer that new returned. This I believe