The delete reserved word from library<new>

I deleted explicitly the array texts, but even after that, I can still access to its values. it doesn’t make sense to me, may someone please explain me??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<string>
#include<new>

int main(){
    std::string* texts = new std::string[3];
    for(int i=0;i<3;i++) {
        std::cout<<"Insert a word: ";
        std::cin>>texts[i];
    }
    for(int i=0;i<3;i++) std::cout<<texts[i]<<'\n'; 
    delete[] texts;
    for(int i=0;i<3;i++) std::cout<<texts[i]<<'\n'; 
    return 0;
}
Last edited on
The memory block is "returned to the heap". The data in the block isn't cleared. However, another call to new may reuse that memory, and the value will change when the owner of that memory uses it.

If you use the values in a deleted block of memory, it's a serious error. It's referred to as dangling memory or a dangling reference.
This article covers memory errors and how to deal with them:
https://aticleworld.com/dangling-pointer-and-memory-leak/
Last edited on
Topic archived. No new replies allowed.