简体   繁体   中英

What does delete[something] do in visual studio?

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; .

As far as I'm concerned you should use delete[] arr . Otherwise you may get Compiler Warning C4208 .

With Microsoft extensions /Ze , you can delete an array using a value within brackets with the delete operator. The value is ignored. Such values are invalid under ANSI compatibility /Za .

If you try to use /Za , you'll get Compiler Error C2203 .

For more details I suggest you could refer to the link: C++ array delete operator syntax

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM