C++ Pointer/Reference problem

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1820 Views - Last Post: 14 August 2011 - 09:43 AM Rate Topic: -----

#1 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

C++ Pointer/Reference problem

Posted 12 August 2011 - 08:32 PM

I am trying to make a Snake game and haven't really used pointers in a while and I guess I need a brush up ;) I'm getting an access violation when I try to run this, can someone please tell me what is wrong?

#include <iostream>
#include <Windows.h>
using namespace std;

const int HEIGHT = 20;
const int WIDTH = 40;

class Snake {
public:
	int r[(HEIGHT - 2) * (WIDTH - 2)];
	int c[(HEIGHT - 2) * (WIDTH - 2)];
	int dr, dc, size;
	Snake(int, int);
	void move(int[][WIDTH]);
};

void print(int[][WIDTH]);
void init(int[][WIDTH], Snake*);
void input(Snake*);

int main() {
	Snake snake(1, 1);
	int board[HEIGHT][WIDTH];
	init(board, &snake);
	bool GAME_RUNNING = true;
	while (GAME_RUNNING) {
		input(&snake);
		snake.move(board);
		print(board);
		Sleep(1000);
		system("cls");
	}
	cin.ignore();
	cin.get();
	return 0;
}

void init(int board[][WIDTH], Snake* snake) {
	for (int c = 0; c < WIDTH; c++) {
		board[0][c] = 1;
	}
	for (int r = 1; r < HEIGHT; r++) {
		board[r][0] = 1;
	}
	for (int r = 1; r < HEIGHT; r++) {
		board[r][WIDTH - 1] = 1;
	}
	for (int c = 1; c < WIDTH - 1; c++) {
		board[HEIGHT - 1][c] = 1;
	}
	for (int r = 1; r < HEIGHT - 1; r++) {
		for (int c = 1; c < WIDTH - 1; c++) {
			board[r][c] = 0;
		}
	}
	board[snake->r[0]][snake->c[0]] = 2;
}

void print(int board[][WIDTH]) {
	for (int r = 0; r < HEIGHT; r++) {
		for (int c = 0; c < WIDTH; c++) {
			switch (board[r][c]) {
			case 0:
				cout << " ";
				break;
			case 1:
				cout << "#";
				break;
			case 2:
				cout << "@";
				break;
			}
		}
		cout << endl;
	}
}

void input(Snake* snake) {
	if (GetAsyncKeyState(VK_UP)) {
		snake->dr = 1;
		snake->dc = 0;
	} else if (GetAsyncKeyState(VK_DOWN)) {
		snake->dr = -1;
		snake->dc = 0;
	} else if (GetAsyncKeyState(VK_RIGHT)) {
		snake->dc = 1;
		snake->dr = 0;
	} else if (GetAsyncKeyState(VK_LEFT)) {
		snake->dc = -1;
		snake->dr = 0;
	}
}

Snake::Snake(int r, int c) {
	size = 0;
	this->r[0] = r;
	this->c[0] = c;
}

void Snake::move(int board[][WIDTH]) {
	int nextR = this->r[0] + this->dr;
	int nextC = this->c[0] + this->dc;
	if (board[nextR][nextC] == 0) {
		this->r[0] = nextR;
		this->c[0] = nextC;
	}
}


Thank you in advance, any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Pointer/Reference problem

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: C++ Pointer/Reference problem

Posted 12 August 2011 - 08:46 PM

where is the error occurring? your debugger should tell you what line(s) the issue is occurring on.
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: C++ Pointer/Reference problem

Posted 12 August 2011 - 08:48 PM

My educated guess without running it myself is that you're trying to access index -1 which can occur when you assign nextR/nextC.
Was This Post Helpful? 0
  • +
  • -

#4 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: C++ Pointer/Reference problem

Posted 12 August 2011 - 09:26 PM

I was about to compile this, when I realized that I am running linux. Where are you receiving the access violation? What exactly is the error?
Was This Post Helpful? 0
  • +
  • -

#5 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: C++ Pointer/Reference problem

Posted 12 August 2011 - 09:48 PM

Unhandled exception at 0x00e91ba8 in SnakeGame.exe: 0xC0000005: Access violation reading location 0x33650f0c.

and it has an arrow pointing to this line

if (board[nextR][nextC] == 0) {

Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ Pointer/Reference problem

Posted 12 August 2011 - 09:50 PM

So set breakpoints appropriately, so you can see what values of nextR and nextC cause the exception, and the series of calculations that lead to those values of nextR and nextC.

Do some debugging here.
Was This Post Helpful? 1
  • +
  • -

#7 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: C++ Pointer/Reference problem

Posted 12 August 2011 - 09:58 PM

Okay, I fixed the error. I forgot to initialize dc and dr :(

But now I have encountered a new problem, the player can move right and left but for some reason going down doesn't work...

EDIT: Oops, here is the updated code :)

#include <iostream>
#include <Windows.h>
using namespace std;

const int HEIGHT = 20;
const int WIDTH = 40;

class Snake {
public:
	int r[(HEIGHT - 2) * (WIDTH - 2)];
	int c[(HEIGHT - 2) * (WIDTH - 2)];
	int dr, dc, size;
	Snake(int, int);
	void move(int[][WIDTH]);
};

void print(int[][WIDTH]);
void init(int[][WIDTH], Snake*);
void input(Snake*);

int main() {
	Snake snake(1, 1);
	int board[HEIGHT][WIDTH];
	init(board, &snake);
	bool GAME_RUNNING = true;
	while (GAME_RUNNING) {
		input(&snake);
		snake.move(board);
		print(board);
		Sleep(100);
		system("cls");
	}
	cin.ignore();
	cin.get();
	return 0;
}

void init(int board[][WIDTH], Snake* snake) {
	for (int c = 0; c < WIDTH; c++) {
		board[0][c] = 1;
	}
	for (int r = 1; r < HEIGHT; r++) {
		board[r][0] = 1;
	}
	for (int r = 1; r < HEIGHT; r++) {
		board[r][WIDTH - 1] = 1;
	}
	for (int c = 1; c < WIDTH - 1; c++) {
		board[HEIGHT - 1][c] = 1;
	}
	for (int r = 1; r < HEIGHT - 1; r++) {
		for (int c = 1; c < WIDTH - 1; c++) {
			board[r][c] = 0;
		}
	}
	board[snake->r[0]][snake->c[0]] = 2;
}

void print(int board[][WIDTH]) {
	for (int r = 0; r < HEIGHT; r++) {
		for (int c = 0; c < WIDTH; c++) {
			switch (board[r][c]) {
			case 0:
				cout << " ";
				break;
			case 1:
				cout << "#";
				break;
			case 2:
				cout << "@";
				break;
			}
		}
		cout << endl;
	}
}

void input(Snake* snake) {
	if (GetAsyncKeyState(VK_UP)) {
		snake->dr = 1;
		snake->dc = 0;
	} else if (GetAsyncKeyState(VK_DOWN)) {
		snake->dr = -1;
		snake->dc = 0;
	} else if (GetAsyncKeyState(VK_RIGHT)) {
		snake->dc = 1;
		snake->dr = 0;
	} else if (GetAsyncKeyState(VK_LEFT)) {
		snake->dc = -1;
		snake->dr = 0;
	}
}

Snake::Snake(int r, int c) {
	size = 0;
	dr = 0;
	dc = 1;
	this->r[0] = r;
	this->c[0] = c;
}

void Snake::move(int board[][WIDTH]) {
	int nextR = r[0] + dr;
	int nextC = c[0] + dc;
	if (board[nextR][nextC] == 0) {
		board[r[0]][c[0]] = 0;
		r[0] = nextR;
		c[0] = nextC;
		board[nextR][nextC] = 2;
	}
}

This post has been edited by C++ Programmer: 12 August 2011 - 10:08 PM

Was This Post Helpful? 0
  • +
  • -

#8 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: C++ Pointer/Reference problem

Posted 12 August 2011 - 10:19 PM

I am still looking at the program, but for some reason the down key makes the object move up and the up key makes the object move down. I'll get back to you.
Was This Post Helpful? 0
  • +
  • -

#9 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: C++ Pointer/Reference problem

Posted 12 August 2011 - 10:22 PM

I fixed it :)

Revised code that works if anyone wants to see the changes.

#include <iostream>
#include <Windows.h>
using namespace std;

const int HEIGHT = 20;
const int WIDTH = 40;

class Snake {
public:
	int r[(HEIGHT - 2) * (WIDTH - 2)];
	int c[(HEIGHT - 2) * (WIDTH - 2)];
	int dr, dc, size;
	Snake(int, int);
	void move(int[][WIDTH]);
};

void print(int[][WIDTH]);
void init(int[][WIDTH], Snake*);
void input(Snake*);

int main() {
	Snake snake(1, 1);
	int board[HEIGHT][WIDTH];
	init(board, &snake);
	bool GAME_RUNNING = true;
	while (GAME_RUNNING) {
		input(&snake);
		snake.move(board);
		print(board);
		Sleep(100);
		system("cls");
	}
	cin.ignore();
	cin.get();
	return 0;
}

void init(int board[][WIDTH], Snake* snake) {
	for (int c = 0; c < WIDTH; c++) {
		board[0][c] = 1;
	}
	for (int r = 1; r < HEIGHT; r++) {
		board[r][0] = 1;
	}
	for (int r = 1; r < HEIGHT; r++) {
		board[r][WIDTH - 1] = 1;
	}
	for (int c = 1; c < WIDTH - 1; c++) {
		board[HEIGHT - 1][c] = 1;
	}
	for (int r = 1; r < HEIGHT - 1; r++) {
		for (int c = 1; c < WIDTH - 1; c++) {
			board[r][c] = 0;
		}
	}
	board[snake->r[0]][snake->c[0]] = 2;
}

void print(int board[][WIDTH]) {
	for (int r = 0; r < HEIGHT; r++) {
		for (int c = 0; c < WIDTH; c++) {
			switch (board[r][c]) {
			case 0:
				cout << " ";
				break;
			case 1:
				cout << "#";
				break;
			case 2:
				cout << "@";
				break;
			}
		}
		cout << endl;
	}
}

void input(Snake* snake) {
	if (GetAsyncKeyState(VK_UP)) {
		snake->dr = -1;
		snake->dc = 0;
	} else if (GetAsyncKeyState(VK_DOWN)) {
		snake->dr = 1;
		snake->dc = 0;
	} else if (GetAsyncKeyState(VK_RIGHT)) {
		snake->dc = 1;
		snake->dr = 0;
	} else if (GetAsyncKeyState(VK_LEFT)) {
		snake->dc = -1;
		snake->dr = 0;
	}
}

Snake::Snake(int r, int c) {
	size = 1;
	dr = 0;
	dc = 1;
	this->r[0] = r;
	this->c[0] = c;
}

void Snake::move(int board[][WIDTH]) {
	int nextR = r[0] + dr;
	int nextC = c[0] + dc;
	if (board[nextR][nextC] == 0) {
		board[r[0]][c[0]] = 0;
		r[0] = nextR;
		c[0] = nextC;
		board[nextR][nextC] = 2;
	}
}

This post has been edited by C++ Programmer: 12 August 2011 - 10:23 PM

Was This Post Helpful? 0
  • +
  • -

#10 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: C++ Pointer/Reference problem

Posted 12 August 2011 - 10:26 PM

I got the same result by switching up to -1 and down to 1, but this was only based on an educated guess given the nature of the problem. I hope you were able to track down the reason for needing the switch.

-alias
Was This Post Helpful? 0
  • +
  • -

#11 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: C++ Pointer/Reference problem

Posted 12 August 2011 - 10:32 PM

Yes I was, the very top left corner would be considered (0, 0) so up needs to change dr to -1 in order to go up and down needs to change dr to 1.
Was This Post Helpful? 0
  • +
  • -

#12 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: C++ Pointer/Reference problem

Posted 12 August 2011 - 10:38 PM

Great, I am glad that you were able to figure it out.
Was This Post Helpful? 0
  • +
  • -

#13 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: C++ Pointer/Reference problem

Posted 12 August 2011 - 11:04 PM

Yet another problem :/

So basically what happens is right in the beginning it flips out but if you press a key, then it starts working the way it should...hmm

I'm sorry I'm asking for so much help but I can't figure this out, been trying for 20min. It's probably something really simple and stupid and I'm just looking too hard into it.

Does anyone see anything that it could be? If you see it, you don't have to give me the answer, just give me a hint.

#include <iostream>
#include <Windows.h>
using namespace std;

const int HEIGHT = 20;
const int WIDTH = 40;

class Snake {
public:
	int r[(HEIGHT - 2) * (WIDTH - 2)];
	int c[(HEIGHT - 2) * (WIDTH - 2)];
	int dr, dc, size;
	Snake(int, int);
	void move(int[][WIDTH]);
};

void print(int[][WIDTH]);
void init(int[][WIDTH], Snake*);
void input(Snake*);

int main() {
	Snake snake(10, 10);
	int board[HEIGHT][WIDTH];
	init(board, &snake);
	bool GAME_RUNNING = true;
	while (GAME_RUNNING) {
		input(&snake);
		snake.move(board);
		print(board);
		Sleep(100);
		system("cls");
	}
	cin.ignore();
	cin.get();
	return 0;
}

void init(int board[][WIDTH], Snake* snake) {
	for (int c = 0; c < WIDTH; c++) {
		board[0][c] = 1;
	}
	for (int r = 1; r < HEIGHT; r++) {
		board[r][0] = 1;
	}
	for (int r = 1; r < HEIGHT; r++) {
		board[r][WIDTH - 1] = 1;
	}
	for (int c = 1; c < WIDTH - 1; c++) {
		board[HEIGHT - 1][c] = 1;
	}
	for (int r = 1; r < HEIGHT - 1; r++) {
		for (int c = 1; c < WIDTH - 1; c++) {
			board[r][c] = 0;
		}
	}
	board[snake->r[0]][snake->c[0]] = 2;
	for (int i = 1; i < snake->size; i++) {
		board[snake->r[i]][snake->c[i]] = 3;
	}
}

void print(int board[][WIDTH]) {
	for (int r = 0; r < HEIGHT; r++) {
		for (int c = 0; c < WIDTH; c++) {
			switch (board[r][c]) {
			case 0:
				cout << " ";
				break;
			case 1:
				cout << "#";
				break;
			case 2:
				cout << "@";
				break;
			case 3:
				cout << "*";
				break;
			}
		}
		cout << endl;
	}
}

void input(Snake* snake) {
	if (GetAsyncKeyState(VK_UP)) {
		snake->dr = -1;
		snake->dc = 0;
	} else if (GetAsyncKeyState(VK_DOWN)) {
		snake->dr = 1;
		snake->dc = 0;
	} else if (GetAsyncKeyState(VK_RIGHT)) {
		snake->dc = 1;
		snake->dr = 0;
	} else if (GetAsyncKeyState(VK_LEFT)) {
		snake->dc = -1;
		snake->dr = 0;
	}
}

Snake::Snake(int r, int c) {
	size = 5;
	dr = 0;
	dc = r;
	this->r[0] = r;
	this->c[0] = c;
	for (int i = 1; i < size; i++) {
		this->r[i] = r;
		this->c[i] = c - i;
	}
}

void Snake::move(int board[][WIDTH]) {
	board[r[size - 1]][c[size - 1]] = 0;
	for (int i = size - 1; i > 0; i--) {
		r[i] = r[(i - 1)];
		c[i] = c[(i - 1)];
		board[r[i]][c[i]] = 3;
	}
	int nextR = r[0] + dr;
	int nextC = c[0] + dc;
	if (board[nextR][nextC] == 0) {
		r[0] = nextR;
		c[0] = nextC;
		board[nextR][nextC] = 2;
	}
}

Was This Post Helpful? 0
  • +
  • -

#14 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: C++ Pointer/Reference problem

Posted 13 August 2011 - 12:21 AM

In your if ... else if sequence in input, you don't do anything if none of the keys are pressed, meaning dr and dc have an unknown value. Just add an else clause that sets dr and dc to 0.

This post has been edited by PlasticineGuy: 13 August 2011 - 12:21 AM

Was This Post Helpful? 0
  • +
  • -

#15 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: C++ Pointer/Reference problem

Posted 13 August 2011 - 12:44 AM

No, because then the Snake would not always move. In the Snake's constructor I initialize dr and dc.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2