new char [n]和new(char [n])之间的差异 [英] differences between new char[n] and new (char[n])
问题描述
new char [n]
和 new(char [n])
p>
我有一个生成代码的第二种情况,g ++(4.8.0)给我
code> ISO C ++不支持可变长度数组类型[-Wvla]
-
new char [n]
表示分配char
类型的对象n
。 - c $ c> new(char [n])表示分配类型
的数组1的对象
? $ b - 删除第一个是清除。
- 应删除第二个与
delete
或delete []
? - 我应该注意什么其他差异吗?
- 我可以安全地删除括号和将第二种情况转换为第一种情况,当软件的其他部分期望第二种情况时?
代码由第三方软件(并由软件的其他部分使用),所以我不能只是使用向量。
这是最小的例子:
int main(void)
{
int n(10);
int * arr = new(int [n]); //删除括号修复警告
* arr = 0; //没有未使用的变量警告
return 0;
}
C ++不允许在类型中使用数组绑定 [n]
,除非 n
是常量表达式。 g ++和一些其他编译器有时会允许它,但是当你开始混合可变长度数组和模板时,不可能得到一致的行为。
明显的异常 int * p = new int [n];
的工作原理是因为 [n]
不是 new
和 new
提供的类型的一部分
//可以在C ++ 11中使用constexpr :
和
const int C = 12;
int main(){
int * p1 = new int [C];
int * p2 = new(int [C]);
typedef int arrtype [C];
int * p3 = new arrtype;
int n = 10;
int * p4 = new int [n];
// int * p5 = new(int [n]); //非法!
// typedef int arrtype2 [n]; //非法!
// int * p6 = new arrtype2;
delete [] p1;
delete [] p2;
delete [] p3;
delete [] p4;语法上,在任何最后的[C]之后,
用于将类型转换为数组类型,新表达式只关心它是否处理数组。所有关于表达式类型的要求,是否使用new []
和delete []
例如当分配的类型是数组时,而不是当使用数组新语法时。因此,在上面的示例中,p1
,p2
和p3
都是等效的,并且在所有情况下delete []
是正确的释放形式。
p4
的初始化有效,但p5
和p6
是不正确的C ++。当不使用-pedantic
时,g ++会允许它们,类似地,我希望p4
code> p5p6
也都是等价的。 @ MM的反汇编支持这个结论。
所以是的,这是一个安全的改进,从这种表达式中删除额外的括号。正确的删除是
delete []
类型。Is there any difference between
new char[n]
andnew (char[n])
?I have the second case in a generated code, g++ (4.8.0) gives me
ISO C++ does not support variable-length array types [-Wvla]
This makes me think if these two are the same or not.
new char[n]
means "allocaten
objects of typechar
.- does
new (char[n])
mean "allocate 1 object of typearray of n chars
"?- deleting the first is clear.
- should I delete the second with
delete
ordelete[]
?- are there any other differences I should be aware of?
- may I safely remove the parentheses and turn the second case into the first, when other parts of the software expect the second?
The code is generated by a third party software (and used by other parts of the software), so I cannot just "use vector instead".
This is minimal example:
int main (void) { int n(10); int *arr = new (int[n]); // removing parentheses fixes warning *arr = 0; // no "unused variable" warning return 0; }
解决方案The basic issue here is that C++ does not allow an array bound
[n]
to be used in a type unlessn
is a constant expression. g++ and some other compilers will sometimes allow it anyway, but it's impossible to get consistent behavior when you start mixing variable-length-arrays and templates.The apparent exception
int* p = new int[n];
works because here the[n]
is syntactically part of thenew
expression, not part of the type provided to thenew
, andnew
does "know how" to create arrays with length determined at runtime.// can be "constexpr" in C++11: const int C = 12; int main() { int* p1 = new int[C]; int* p2 = new (int[C]); typedef int arrtype[C]; int* p3 = new arrtype; int n = 10; int* p4 = new int[n]; // int* p5 = new (int[n]); // Illegal! // typedef int arrtype2[n]; // Illegal! // int* p6 = new arrtype2; delete[] p1; delete[] p2; delete[] p3; delete[] p4; }
Semantically, though, after any final
[C]
is used to convert a type into an array type, the new expression only cares about whether it's dealing with an array or not. All the requirements about type of the expression, whether to usenew[]
anddelete[]
, and so on say things like "when the allocated type is an array", not "when the array new syntax is used". So in the example above, the initializations ofp1
,p2
, andp3
are all equivalent, and in all casesdelete[]
is the correct deallocation form.The initialization of
p4
is valid, but the code forp5
andp6
is not correct C++. g++ would allow them anyway when not using-pedantic
, and by analogy I'd expect the initializations forp4
,p5
, andp6
to also all be equivalent. @MM's disassembly supports that conclusion.So yes, it should be a safe improvement to remove the "extra" parentheses from this sort of expression. And the correct deletion is the
delete[]
type.这篇关于new char [n]和new(char [n])之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!