Your example "works" (not really because its still memory that isnt valid) but this doesnt:
Code:
#include<iostream>
using namespace std;
class CCC
{
public:
int num;
};
int main()
{
//test1 ***
int outerA;
if(true)
{
int i = 10;
outerA = i;
}
cout<<outerA<<endl;
//******************
//test2 ***
int* outerB;
if(true)
{
int my_array[3] = {1, 2, 3};
outerB = my_array;
}
int my_array2[3] = { 8,6,7 };
cout<<outerB[0]<<endl;
//*******************
//test3 *****
CCC* outerC;
if(true)
{
CCC a;
a.num=10;
outerC = &a;
}
CCC b;
b.num = 80;
cout<<outerC->num<<endl;
cin.get();
return 0;
}
On my computer this gives me an output of:
10
8
80
So we can clearly see that the memory where my_array[3] is stored has been overwritten by the my_array2[3], same with CCC a and CCC b.