提问者:小点点

C++Delete[]运算符


#include <iostream>

using namespace std;

int main()
{
    float* temps = new float[10];

    float* temps2 = temps;

    delete[] temps2;

    return 0;
}

与。。。工作相同

#include <iostream>

using namespace std;

int main()
{
    float* temps = new float[10];

    float* temps2 = temps;

    delete[] temps;

    return 0;
}

它们都释放了所有分配的内存吗? 还是我必须删除[]原来的指针?


共1个答案

匿名用户

两个都很好。

只要指针具有完全相同的类型(允许对const进行更改),就可以调用delete[]

(请注意,对于newdelete,指针可以是多态相关的,但对于new[]delete[]则并非如此)。

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++delete|运算符)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?