-2

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.

tortuga
  • 737
  • 2
  • 13
  • 34
  • Also, don't miss the hilarious answer to [Can a local variable's memory be accessed outside its scope?](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – Bo Persson Sep 08 '15 at 19:16
  • Hi, thanks for the link. What I'm confused about is, since my arr pointer is a global variable, and I use it to point to an array I allocate memory to n initialise dynamically. In the end function when I delete the array(allocated in initialise function), doesn't the pointer to that array also gets deleted ? Or only the array space(containing 10) variables is getting deleted ? – tortuga Sep 08 '15 at 19:27
  • 1
    The pointer variable is still there, and likely contains the same bits it did before. However, the place it pointed to doesn't have to be there anymore, and you are not allowed to try to access it. I have used machines where the last `arr[9]` could have terminated the program. – Bo Persson Sep 09 '15 at 06:27
  • Yeah. Got it. thanks – tortuga Sep 09 '15 at 07:40

1 Answers1

1

Accesing deleted part of memory causes Undefined Behavior. It means anything can happen. Although the memory is deallocated so something else can be written there, in this case it is not, so you can still read a value that was there before.

Of course you shouldn't even try to. It is a good practice to set pointer to NULL after it is deleted.

Estiny
  • 888
  • 1
  • 7
  • 20
  • So, does this mean that my code and logic for dynamically allocating and deleting the array, and using it in different blocks is true ? I was just printing 'arr[9]' value to check if the array got deleted or not. When it printed the value, I got confused – tortuga Sep 08 '15 at 19:11
  • It looks correct. Try allocating some memory, and then calling `cout< – Estiny Sep 08 '15 at 19:14
  • gives 9 as output then also. – tortuga Sep 08 '15 at 19:17
  • Yeah, Undefined Behavior is undefined. Anything can happen. Still you can refer to link given by Bo Persson to see it is expected. – Estiny Sep 08 '15 at 19:20