gpt4 book ai didi

c++ - 在 Visual Studio 中删除[某事]有什么作用?

转载 作者:行者123 更新时间:2023-12-02 20:52:16 24 4
gpt4 key购买 nike

我知道这是 C++ 标准不允许的,并且它不能在 gcc 上编译,但我想知道为什么它可以在 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;
}

其行为与 delete[] arr;delete[n+5] arr;delete[n/2] arr; 相同code>、delete[-54] arr; 甚至 delete[A{}] arr;

最佳答案

就我而言,您应该使用delete[] arr。否则你可能会得到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.

如果您尝试使用/Za,您将得到Compiler Error C2203 .

欲了解更多详情,我建议您可以引用以下链接: C++ array delete operator syntax

关于c++ - 在 Visual Studio 中删除[某事]有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60049105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com