Delete[] vs delete

if I were to call delete[] on a pointer where I had only called new type instead of new type [], how bad would that be?

1
2
3
int * a;
a = new int;
delete[] a;
The behavior of such a program would be undefined, meaning any of these are possible:
* The memory may be released normally, as with delete.
* The program may crash.
* The program may enter an invalid state and crash at an arbitrary point in the future.
* The program may enter an invalid state and silently corrupt the user's data without crashing.
* The program may inadvertently cause the instruction pointer to jump to writable memory, which an attacker could use to execute arbitrary code in the user's computer to take control of it.

Thankfully, in modern C++ explicitly using new and delete is almost never necessary anymore.
Last edited on
Undefined behavior.

By undefined, that can result in anything from absolutely no harm to segmentation fault (*Nix, Windows has a different name for the same thing, both result in program termination).

Never rely upon or use undefined behavior. Ever.

You can show numerous examples where code snippets demonstrate undefined behavior that is quite harmless in that circumstance, but change one little seemingly irrelevant circumstance, possibly even in entirely unrelated code, and it will explode.

Any argument posing to use undefined behavior is a trip down the rabbit hole. It serves no purpose to explore it, really.

It is the programming equivalent of "hold my beer, and watch this".

Alright, thank you! :)
Topic archived. No new replies allowed.