1

I'm trying to fill an array of int array to create a tilemap for a videogame. However I've used switch case and I tried to fill the map when initializing it but some of the number are random for an unknown reason.

Here's the code:

int **Process::get_story_map(int lvl)
{
    int **map = new int *[10];
    switch (lvl)
    {
        case (0): {
            int line01[10] = {5, 0, 1, 1, 2, 2, 1, 1, 0, 5};
            int line02[10] = {0, 0, 1, 1, 2, 2, 1, 1, 0, 0};
            int line03[10] = {1, 1, 1, 1, 2, 2, 1, 1, 1, 1};
            int line04[10] = {1, 1, 1, 1, 0, 0, 1, 1, 1, 1};
            int line05[10] = {2, 2, 1, 0, 0, 0, 0, 1, 2, 2};
            int line06[10] = {2, 2, 1, 0, 0, 0, 0, 1, 2, 2};
            int line07[10] = {1, 1, 1, 1, 0, 0, 1, 1, 1, 1};
            int line08[10] = {1, 1, 1, 1, 2, 2, 1, 1, 1, 1};
            int line09[10] = {0, 0, 1, 1, 2, 2, 1, 1, 0, 0};
            int line10[10] = {5, 0, 1, 1, 2, 2, 1, 1, 0, 5};
            map[0] = line01;
            map[1] = line02;
            map[2] = line03;
            map[3] = line04;
            map[4] = line05;
            map[5] = line06;
            map[6] = line07;
            map[7] = line08;
            map[8] = line09;
            map[9] = line10;
        }
        case (1): {
            int line01[10] = {5, 0, 1, 1, 1, 1, 1, 1, 0, 5};
            int line02[10] = {0, 0, 1, 1, 2, 2, 1, 1, 0, 0};
            int line03[10] = {1, 1, 1, 1, 2, 2, 1, 1, 1, 1};
            int line04[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
            int line05[10] = {2, 2, 2, 1, 2, 2, 1, 2, 2, 2};
            int line06[10] = {2, 2, 2, 1, 2, 2, 1, 2, 2, 2};
            int line07[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
            int line08[10] = {1, 1, 1, 1, 2, 2, 1, 1, 1, 1};
            int line09[10] = {0, 0, 1, 1, 2, 2, 1, 1, 0, 0};
            int line10[10] = {5, 0, 1, 1, 2, 2, 1, 1, 0, 5};
            map[0] = line01;
            map[1] = line02;
            map[2] = line03;
            map[3] = line04;
            map[4] = line05;
            map[5] = line06;
            map[6] = line07;
            map[7] = line08;
            map[8] = line09;
            map[9] = line10;
            break;
        }
        default:
            return (NULL);
    }
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            std::cout << map[i][j] << " ";
        std::cout << std::endl;
    }
    std::cout << "--------------------------------" << std::endl;
    return (map);
}

Here's where I call it

int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    Process *process = new Process();
    // int **map = process->generate_arcade_map();
    int **map = new int *[10];
    map = process->get_story_map(1);
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            std::cout << map[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

And here's the output:

5 0 1 1 1 1 1 1 0 5 
0 0 1 1 2 2 1 1 0 0 
1 1 1 1 2 2 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 
2 2 2 1 2 2 1 2 2 2 
2 2 2 1 2 2 1 2 2 2 
1 1 1 1 1 1 1 1 1 1 
1 1 1 1 2 2 1 1 1 1 
0 0 1 1 2 2 1 1 0 0 
5 0 1 1 2 2 1 1 0 5 
--------------------------------
5 0 1 1 1 1 1 1 0 5 
0 0 1 1 2 2 -2070125006 32711 0 0 
1 1 -2068490400 32711 5 0 1 0 1 0 
-2068507136 32711 -2070171513 32711 1 1 0 0 -116061664 32766 
-2066141792 32711 32711 0 0 0 -2068490400 32711 1 0 
1 0 -1835315192 22061 -2068507136 32711 -2070171513 32711 53 0 
3432 22061 -1835306944 22061 1 0 0 0 -116061680 32766 
-1835322135 22061 0 0 -1835306944 22061 -1835306936 22061 -116061695 32766 
-1835306944 32711 -125382144 99592016 1 2 0 0 -1835315192 22061 
-116061152 22061 0 0 -2062934976 32711 -2067049860 32711 -1835322135 22061

I don't see what's causing this problem since the first few numbers are right. (I know my code looks kinda spaghetti and I'm open to any advice :D)

BLSPR
  • 567
  • 1
  • 3
  • 13

1 Answers1

2
{
    case (0): {
        int line01[10] = {5, 0, 1, 1, 2, 2, 1, 1, 0, 5};
        int line02[10] = {0, 0, 1, 1, 2, 2, 1, 1, 0, 0};
        int line03[10] = {1, 1, 1, 1, 2, 2, 1, 1, 1, 1};
        int line04[10] = {1, 1, 1, 1, 0, 0, 1, 1, 1, 1};
        int line05[10] = {2, 2, 1, 0, 0, 0, 0, 1, 2, 2};
        int line06[10] = {2, 2, 1, 0, 0, 0, 0, 1, 2, 2};
        int line07[10] = {1, 1, 1, 1, 0, 0, 1, 1, 1, 1};
        int line08[10] = {1, 1, 1, 1, 2, 2, 1, 1, 1, 1};
        int line09[10] = {0, 0, 1, 1, 2, 2, 1, 1, 0, 0};
        int line10[10] = {5, 0, 1, 1, 2, 2, 1, 1, 0, 5};
        map[0] = line01;
        map[1] = line02;
        map[2] = line03;
        map[3] = line04;
        map[4] = line05;
        map[5] = line06;
        map[6] = line07;
        map[7] = line08;
        map[8] = line09;
        map[9] = line10;
    }

You are storing pointers to local objects. As soon as you hit the }, line01 no longer exists because it is no longer in scope. So map[0] contains a pointer to nothing valid.

You should never dereference a pointer to an object that no longer exists. You can only access objects during their lifetimes.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • That makes sense thanks ! Is there any way I could do it so it still exits after exiting the switch case ? And do you know if there is a reason some numbers stays the same or it just unpredictable behavior ? – BLSPR Jun 06 '22 at 21:04
  • 1
    For the shown case, it is simple: just declare the variables `static` (and optionally also `const`). – j6t Jun 06 '22 at 21:06