作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道这是 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/
我是一名优秀的程序员,十分优秀!