DarkoNaito_09
DarkoNaito_09

Reputation: 101

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?

Upvotes: 0

Views: 125

Answers (1)

Bathsheba
Bathsheba

Reputation: 234705

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[]).

Upvotes: 4

Related Questions