my C++ is super rusty, how come the locally declared 'out' map still works outside it's scope? Is it legit? Shouldn't it be dynamically declared with "new"?
// Example program with a function returning a map
#include <iostream>
#include <string>
#include <map>
std::map<std::string, int> function()
{
std::map<std::string, int> out;
out["one"] = 1;
out["two"] = 2;
std::cout << &out << std::endl;
return out;
}
int main()
{
std::map<std::string, int> out1 = function();
std::cout << &out1 << std::endl;
for (const auto & iter : out1)
std::cout << iter.first << " = " << iter.second << std::endl;
}
When I run it:
0x7ffd89f354a0
0x7ffd89f354a0
one = 1
two = 2
It seems like they point to the same memory address