I have the following code :
#include <iostream>
#include <string>
using namespace std;
int *arr;
void initiate(int n)
{
    cout<<"inside initiate"<<endl;
    arr = new int [10];
    for(int i=0;i<10;i++)
        arr[i] = i;
}
void end(int *arr)
{
    cout<<"inside end"<<endl;
    delete[] arr;
}
int main() {
    string str;
    cin>>str;
    while(str != "End")
    {
        if(str == "Insertion")
        {
            cout<<"inside if"<<endl;
            initiate(10);
        }
        cin>>str;
        if(str=="Selection")
        {
            cout<<"inside selection"<<endl;
            cout<<arr[9]<<endl;
        }
        cin>>str;
    }
    end(arr);
    cout<<arr[9];
    return 0;
}
I try to allocate storage to array dynamically via one function, and delete it via other function depending on the input by the user. For the following set of inputs :
Insertion Selection End
I get the following output. :
inside if inside initiate inside selection 9 inside end 9
What I'm unable to fathom is why my program is printing the arr[9] value, after the end function has been called. According to my understanding, since I deleted the array by calling end function before this, I should get and error or something.
Any help is appreciated.
 
    