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

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

Post just about everything that isn't directly related to Spring here!

Moderator: Moderators

Post Reply
User avatar
Ling_Lover
Posts: 100
Joined: 26 Sep 2006, 11:50

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

Post 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++...
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Post 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;
}
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

Or instead do away with the 2D aspect entirely and use a 1 dimensional array where the index is y*width+x
User avatar
Ling_Lover
Posts: 100
Joined: 26 Sep 2006, 11:50

Post 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)
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

Just remember that any half decent compiler will reorganize it into a 1 dimensional array at compile time.
Post Reply

Return to “Off Topic Discussion”