I have a function that puts a pointer into 10x10 std::array of pointers. I made a global vector that holds ints and have a function that creates new ints, puts them into vector and assigns their address in the grid. When I try to access grid, I get junk value even though all my values should be initialized.
I'm not sure where is my mistake. I checked the vector and all my created ships are there. Since my objects are still alive then I don't see why my pointer is not working.
#include <iostream>
#include <array>
#include <vector>
using namespace std;
using grid = array<array<int*, 10>, 10>;
vector<int> my_special_ints{};
grid fill_grid(int* val) {
grid g;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
g[i][j] = val;
}
}
return g;
}
void modifying_function(grid& g) {
for (int i = 0; i < 10; i++) {
int temp_int = i + 10;
my_special_ints.push_back(temp_int);
g[i][0] = &my_special_ints.back();
}
}
int main()
{
int empty_int = -1;
grid ptr_arr = fill_grid(&empty_int);
modifying_function(ptr_arr);
cout << *ptr_arr[1][0]; // junk value
}