Here I have some c++ code all in 1 cpp file:
struct Object
{
int* data;
}
Object obj;
void pizza()
{
int joe[] = {5, 3, 4, 7, 8};
obj.data = joe;
}
int main()
{
pizza();
std::cout << obj.data[2];
return 0;
}
obj.data is just a pointer to a array of ints, and I initialize it in the pizza function, so by the time it leaves this function obj.data should be pointing to NULL becuase this data only existed in the scope of pizza.
But this is not the case, when I std::cout a value of this array the data is still there even though the pointer points to data that is no longer valid.
How does this work, is the data being copied? Is this safe or can it lead to problems?