-3
#include <iostream>
#include <string>

using namespace std;
void print(string data[], int size) {
cout << "Print array" << endl;
for (int i=0; i<size; i++) {
cout << data[i] << ", ";
}
cout << endl;
}

int main(int argc, char** argv) {
string arr1[] = {"Apple", "Banana", "Coconut", "Durian"};
string *arr2 = new string[6];

arr2[4] = "Eggplant";
arr2[5] = "AppleBanana";

std::copy(std::begin(arr1), std::end(arr1), arr2);
print(arr2, 6);
delete[] arr2;
print(arr2, 6);
return 0;
}

I was wondering why delete[] arr2; only free up the arr2[0] because after I print it again only the first element in arr2 is freed but the rest still got the elements I don't quite understand the free()/delete yet so it would be nice if someone also help explain it. I have been asking professor about it and I'm still confused as to why freeing up arr2 still got elements in its

Powzer
  • 19
  • 5
  • 3
    Access to deleted memory is UB (undefined behavior). You should not access it after deleting and if you do so, anything can happen. – Afshin Aug 20 '22 at 08:25
  • 1
    Freeing memory simply tells your system that you do not need it anymore, it does not necessarily change the memory. Accessing it after freeing it is called `use after free` and is undefined behaviour. – jacky la mouette Aug 20 '22 at 08:29
  • 1
    Its similar behavior with when you delete something on your computer. The OS just keeps that memory as 'free to write'. If you access it before you ovewrite it you can get your files back. Thats why many people use [shred](https://www.computerhope.com/unix/shred.htm) to actually delete something. – laegirl Aug 20 '22 at 08:29
  • 1
    About the first element going away: The memory manager has to store some administrative data about the freed block, so it can be found and reused later. And it often stores that data in the block itself. Because it can. – BoP Aug 20 '22 at 08:55
  • @Afshin ohhh so it means that it completely freed up the memory and what's left after that shouldn't be use right? does it mean that after I delete or free() it it will not have any value anymore? – Powzer Aug 20 '22 at 09:06
  • 1
    `delete[]` is guaranteed no-throw, so if you induce UB by accessing deleted arrays, the program won't let you know with an error. Instead it will keep running, as in this case, and most probably produce invalid results. – Giogre Aug 20 '22 at 09:07
  • Another term for this is "use-after-free". – JHBonarius Aug 20 '22 at 09:08
  • _"does it mean that after I delete or free() it it will not have any value anymore?"_ no, it doesn't. That's what people are trying to say. It can be in any state. You should only not access it, as that will cause any kind of problems. – JHBonarius Aug 20 '22 at 09:09
  • @JHBonarius Thank you so much! that link explained it – Powzer Aug 20 '22 at 09:15
  • so it basically freed the memory that is preserved for the assigned pointer and not the pointer itself the pointer itself remained – Powzer Aug 20 '22 at 09:16
  • @Powzer - *"the pointer itself remained"*. Yes, and No. The bits in the pointer may remain the same. But its status has changed from "valid pointer" to "invalid pointer". And you are not allowed to use an invalid pointer. – BoP Aug 20 '22 at 09:51
  • `delete` doesn't do a cleanup because that would be slower and C++ assumes you know what you're doing (not accessing after deleting) so a cleanup is not needed. Of course, we already know that people do make mistakes and not cleaning up can make troubleshooting more difficult. By the way, when using smart pointers, you don't do the delete yourself anymore, it is deleted at the time it can't be accessed anymore and the invalid pointer problem doesn't exist anymore. – stefaanv Aug 20 '22 at 10:43
  • Crashes on my machine. That's the nature of **undefined behavior**: anything can happen. If you're lucky, it'll crash. If you are particularly unlucky, it may appear to work. – Eljay Aug 20 '22 at 13:34

1 Answers1

-6

Maybe, it is better to delete array in this way

for(int i = 0; i < size; i++)
{
   delete [] arr2[i];
   arr2[i] = nullptr;
}

Also you are not allowed to use the deleted arr2 after you used the keyword delete because it will cause some problems in time of and after compilation