0

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?

DarkoNaito_09
  • 71
  • 2
  • 12
  • Not the best dupe - I've reopened - the rules differ slightly for `delete` and `delete[]` and the duplicate was for the former. – Bathsheba Jul 13 '20 at 12:41

1 Answers1

4

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

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thanks. Btw, how does delete[] know the size of the dynamic array? – DarkoNaito_09 Jul 13 '20 at 12:35
  • 1
    @DarkoNaito_09: A C++ runtime will maintain a table of allocated sizes somewhere. – Bathsheba Jul 13 '20 at 12:37
  • so why doesn't c++ provide a feature to get the size of an array? – DarkoNaito_09 Jul 13 '20 at 12:38
  • 2
    @DarkoNaito_09: Over-allocation is one reason. If you ask for say 7 elements there's a reasonable chance the runtime might allocate 8. Then it would have to remember that you only wanted 7. – Bathsheba Jul 13 '20 at 12:43
  • 1
    @Bathsheba: I very much doubt that such a table exists. I always assumed that the size was stored in the area preceding the array. (g++ stores the size in the preceding word, at least for arrays of built-in type.) – TonyK Jul 13 '20 at 12:46
  • 1
    @TonyK: You're right. I wish I could edit my previous comment. Silly SO comment edit rules... I could run for moderator, I guess and if successful, make the comment edit, then resign. – Bathsheba Jul 13 '20 at 12:47
  • The main reason is that not all pointers will point at an array, dynamically allocated or otherwise. All means of retrieving the size of an array given a pointer will make assumptions about what that pointer points at - and, if the assumption made is invalid, the behaviour will be undefined. There are so many things a pointer can point at - not all of which are associated with a retrievable size - that trying to specify such things would be difficult, let alone trying to write code to retrieve the size correctly. – Peter Jul 13 '20 at 12:47
  • 1
    @TonyK • on my platform, `new[]` stores the size (in terms of object count) as a pair (pointer, count) in the allocated memory table of the `new[]` heap. It isn't stored in the area preceding the array. But that's an implementation detail, and can vary by compiler & platform. (My platform, with a diagnostic build, also puts in preceding and trailing memory set to a particular byte value, to detect write underruns and overruns.) – Eljay Jul 13 '20 at 12:57