new char [n]和new(char [n])之间的差异 [英] differences between new char[n] and new (char[n])

查看:314
本文介绍了new char [n]和new(char [n])之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

new char [n] new(char [n]) p>

我有一个生成代码的第二种情况,g ++(4.8.0)给我

 code> ISO C ++不支持可变长度数组类型[-Wvla] 


  1. new char [n] 表示分配 char 类型的对象 n

  2. c $ c> new(char [n])表示分配类型的数组1的对象? $ b
  3. 删除第一个是清除。

  4. 应删除第二个与 delete delete []

  5. 我应该注意什么其他差异吗?

  6. 我可以安全地删除括号和将第二种情况转换为第一种情况,当软件的其他部分期望第二种情况时?

代码由第三方软件(并由软件的其他部分使用),所以我不能只是使用向量。



这是最小的例子:

  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 提供的类型的一部分 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> p5 p6 也都是等价的。 @ MM的反汇编支持这个结论。



所以是的,这是一个安全的改进,从这种表达式中删除额外的括号。正确的删除是 delete [] 类型。


Is there any difference between new char[n] and new (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.

  1. new char[n] means "allocate n objects of type char.
  2. does new (char[n]) mean "allocate 1 object of type array of n chars"?
  3. deleting the first is clear.
  4. should I delete the second with delete or delete[]?
  5. are there any other differences I should be aware of?
  6. 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 unless n 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 the new expression, not part of the type provided to the new, and new 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 use new[] and delete[], 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 of p1, p2, and p3 are all equivalent, and in all cases delete[] is the correct deallocation form.

The initialization of p4 is valid, but the code for p5 and p6 is not correct C++. g++ would allow them anyway when not using -pedantic, and by analogy I'd expect the initializations for p4, p5, and p6 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆