简体   繁体   中英

C++ Delete [] Operator

Does

#include <iostream>

using namespace std;

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

    float* temps2 = temps;

    delete[] temps2;

    return 0;
}

have the same working as

#include <iostream>

using namespace std;

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

    float* temps2 = temps;

    delete[] temps;

    return 0;
}

?

Do they both release all the allocated memory? Or do I have to delete[] the original pointer?

Both are fine.

So long as the pointer has exactly the same type (a change to or from const is allowed), you can call delete[] .

(Note that for new and delete the pointer can be polymorphically related, but that's not true for new[] and delete[] ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM