I have declared an array/variable inside the function. I have been told that memory got allocated/declared in the function body is deallocated automatically when the function call ends. but when I imported the address of the memory allocated in function and dereferenced it in the main, the memory (i mean the value) still stays there. em a bit confused there.
#include <iostream>
using namespace std;
void fn(int *& ptr)
{
int a = 10;
ptr = &a;
}
void fn1(int*& ptr)
{
int a[4] = { 3,4,5,6 };
ptr = a;
}
int main()
{
int* a = new int[5];
for (int i = 00; i < 5; ++i)
a[i] = i + 1;
cout << *a << endl;
a = 0;
fn(a);
cout << *a;
a = 0;
fn1(a);
cout << endl << *a;
}