I know this is not allowed by C++ standard and it does not compile on gcc, but I want to know why it works in Visual Studio.
#include <iostream>
struct A
{
A()
{
std::cout << "A()" << std::endl;
}
~A()
{
std::cout << "~A()" << std::endl;
}
};
int main()
{
int n;
std::cin >> n;
A* arr = new A[n];
delete[n] arr;
}
It behaves the same with delete[] arr;
, delete[n+5] arr;
, delete[n/2] arr;
, delete[-54] arr;
and even delete[A{}] arr;
.