Page 1 of 1

SIGBUS error... could use help... :S

Posted: 31 Mar 2007, 08:24
by Ling_Lover
Hey, I keep getting SIGBUS errors when trying to access this variable outside it's initalising function, and have been for a while... Not sure why...

Can anyone help?

Code: Select all

class square {
	public:
		int unit;
		int terrain;
		bool path;
};

square** squares;
void makesquares() {
	square** squares = new square*[x];
	for (int i = 0; i < y; i++) {
		*(squares + i) = new square[y];
	}
	for (int i = 0; i < x; i++ ) {
		for (int j = 0; j < y; j++ ) {
			squares[i][j].unit = -1;
			squares[i][j].terrain = 0;
			squares[i][j].path = false;
		}
	}
}
P.S. Bear in mind I'm fairly new to C++...

Posted: 31 Mar 2007, 14:08
by Kloot
It's because you declare a second (local) squares variable in makesquares() and initialize only that one, leaving the global squares untouched. What you want is to change this:

Code: Select all

square** squares = new square*[x];
to this:

Code: Select all

squares = new square*[x];
Or better yet, alter makesquares() to return squares and get rid of the global var:

Code: Select all

// (remove this) square** squares;
square** makesquares() {
   square** squares = new square*[x];
   // ...
   return squares;
}

Posted: 31 Mar 2007, 15:16
by AF
Or instead do away with the 2D aspect entirely and use a 1 dimensional array where the index is y*width+x

Posted: 01 Apr 2007, 04:15
by Ling_Lover
Thanks Kloot... It all makes sense after you know the answer...

and AF, yeah, I did think about doing it like that... but I rather it as a 2 dimensional array (for no particular reason, just personal preference)

Posted: 01 Apr 2007, 04:46
by AF
Just remember that any half decent compiler will reorganize it into a 1 dimensional array at compile time.