Gamealishis
Gamealishis

Reputation: 1

Two index's of an array have same memory adress

I was working on a project to practice movement through an array and I found a very weird occurrence. While messing around, I discovered that when I moved my player to the edge of the array, it showed up on the edge and a row beneath it and on the complete opposite column. Showcase of problem

I then added a watch to my program to the point where I assigned the player position. I watched the position in the array I wanted the player set and the location where I didn't want the player set. I found that when one changed, so did the other.

Before | After

Being thoroughly confused, I decided to check their memory locations. On a hunch, I had the idea they may be the same. Turns out, I was right. Proof
Code:

cout << "map[playerY][playerX] Array Adress: " << &map[playerY][playerX] << endl;
cout << "map[playerY + 1][0] Array Adress:   " << &map[playerY + 1][0] << endl;
cout << "map[0][0] Array Adress:             " << &map[0][0] << endl;

system("pause"); 

After many google searches and reviewing many questions on this site, I have no idea why this is happening! If anyone can help, I'd great appreciate it.

Full Code:

#include <iostream>
#include <string>
#include <Windows.h>
#include <vector>
#include <conio.h>

using namespace std;

int map[9][9];
int playerX = 9;
int playerY = 8;

void drawBoard();
void setPlayer();
void playerInput();

int main() {
    playerInput();
}

void drawBoard() {
    //Fills array
    for (int col = 0; col <= 9; col++) {
        for (int row = 0; row <= 9; row++) {
            map[col][row] = 0;
        }
    } 

    cout << "map[playerY][playerX] Array Adress: " << &map[playerY][playerX] << endl;
    cout << "map[playerY + 1][0] Array Adress:   " << &map[playerY + 1][0] << endl;
    cout << "map[0][0] Array Adress:             " << &map[0][0] << endl;

    system("pause");

    setPlayer(); //Sets Player Postion

    //Draws the board
    cout << map[0][0] << " " << map[0][1] << " " << map[0][2] << " " << map[0][3] << " " << map[0][4] << " " << map[0][5] << " " << map[0][6] << " " << map[0][7] << " " << map[0][8] << " " << map[0][9] << endl;
    cout << map[1][0] << " " << map[1][1] << " " << map[1][2] << " " << map[1][3] << " " << map[1][4] << " " << map[1][5] << " " << map[1][6] << " " << map[1][7] << " " << map[1][8] << " " << map[1][9] << endl;
    cout << map[2][0] << " " << map[2][1] << " " << map[2][2] << " " << map[2][3] << " " << map[2][4] << " " << map[2][5] << " " << map[2][6] << " " << map[2][7] << " " << map[2][8] << " " << map[2][9] << endl;
    cout << map[3][0] << " " << map[3][1] << " " << map[3][2] << " " << map[3][3] << " " << map[3][4] << " " << map[3][5] << " " << map[3][6] << " " << map[3][7] << " " << map[3][8] << " " << map[3][9] << endl;
    cout << map[4][0] << " " << map[4][1] << " " << map[4][2] << " " << map[4][3] << " " << map[4][4] << " " << map[4][5] << " " << map[4][6] << " " << map[4][7] << " " << map[4][8] << " " << map[4][9] << endl;
    cout << map[5][0] << " " << map[5][1] << " " << map[5][2] << " " << map[5][3] << " " << map[5][4] << " " << map[5][5] << " " << map[5][6] << " " << map[5][7] << " " << map[5][8] << " " << map[5][9] << endl;
    cout << map[6][0] << " " << map[6][1] << " " << map[6][2] << " " << map[6][3] << " " << map[6][4] << " " << map[6][5] << " " << map[6][6] << " " << map[6][7] << " " << map[6][8] << " " << map[6][9] << endl;
    cout << map[7][0] << " " << map[7][1] << " " << map[7][2] << " " << map[7][3] << " " << map[7][4] << " " << map[7][5] << " " << map[7][6] << " " << map[7][7] << " " << map[7][8] << " " << map[7][9] << endl;
    cout << map[8][0] << " " << map[8][1] << " " << map[8][2] << " " << map[8][3] << " " << map[8][4] << " " << map[8][5] << " " << map[8][6] << " " << map[8][7] << " " << map[8][8] << " " << map[8][9] << endl;
    cout << map[9][0] << " " << map[9][1] << " " << map[9][2] << " " << map[9][3] << " " << map[9][4] << " " << map[9][5] << " " << map[9][6] << " " << map[9][7] << " " << map[9][8] << " " << map[9][9] << endl;

}

void setPlayer() {
    map[playerY][playerX] = 1;
}

void playerInput() {
    char keyPressed;
    while (true) {
        drawBoard();
        //Getting key pressed
        keyPressed = _getch();

        if (keyPressed == 'w' && playerY >= 0) {
            playerY -= 1;
        }
        else if (keyPressed == 's' && playerY <= 9) {
            playerY += 1;
        }
        else if (keyPressed == 'd' && playerX <= 9) {
            playerX += 1;
        }
        else if (keyPressed == 'a' && playerX >= 0) {
            playerX -= 1;
        }
        //Checking player position
        if (playerY < 0) {
            playerY += 1;
        }
        else if (playerY > 9) {
            playerY -= 1;
        }
        else if (playerX > 9) {
            playerX -= 1;
        }
        else if (playerX < 0) {
            playerX += 1;
        }

        system("cls");
    }

}

Upvotes: 0

Views: 43

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122288

Valid indices into

int map[9][9];

are [0...8][0...8]. map[playerY][playerX] == map[8][9] which is out-of-bounds, hence your code has undefined behavior.

Also when you fill the array you are going out of bounds:

for (int col = 0; col <= 9; col++) {
    for (int row = 0; row <= 9; row++) {
        map[col][row] = 0;
    }
} 

The common convention is to use half open intervals, ie [0,9), ie including the lower, but excluding the upper bound. For a 10x10 map that would be

int map[10][10];
for (int col = 0; col < 10; col++) {
    for (int row = 0; row < 10; row++) {
        map[col][row] = 0;
    }
} 

Upvotes: 4

Related Questions