Reputation: 101
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
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