Array of pointers question: segmentation fault

Hello everybody!

I'm getting a segmentation fault, and I'm not sure why. Basically I'm trying to print out a game board where '@' ('iblock' class) is the border, and the inside is just ' ' ('empty' class). I get a segmentation fault after it prints out the top border. For example, i'm trying to get something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@@@@@@@@@@@@@@@@@@@@@@@
@                     @
@                     @
@                     @
@                     @
@                     @
@                     @
@                     @
@                     @
@                     @
@                     @
@                     @
@                     @
@                     @
@                     @
@                     @
@@@@@@@@@@@@@@@@@@@@@@@

but instead, i'm getting this:
1
2
@@@@@@@@@@@@@@@@@@@@@@@
segmentation fault



Here's my code. I'm inheriting class object in the "iblock" class and "empty" class, and am also using a pure virtual function in "object" class for the function "getType()"

int main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main()
{

	object *board[23][40];
	initializeBoard(board);
	for (int a=0; a < 23; a++) //starts printing
	{
		for (int b=0; b < 40; b++)
			cout << board[a][b]->getType();
		cout << endl;
	}
}

void initializeBoard(object *board[][40])
{
	iblock x;
	empty j;
	
	
	for (int a=0; a < 23; a++) //initialize first column
		board[a][0] = &x;
	for (int b=0; b < 23; b++) //initialize last column
		board[b][39] = &x;
	for (int c=0; c < 40; c++) //initialize first row
		board[0][c] = &x;
	for (int d=0; d < 40; d++) //initialize last row
		board[22][d] = &x;
	for (int e=1; e < 22; e++) //initialize blanks for everything else
		for (int f=1; f < 39; f++)
			board[e][f] = &j;
}

object.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef OBJECT_H_
#define OBJECT_H_

#include <iostream>

class object
{
	public:
		object ();
		virtual char getType() = 0;
	
};

#endif 

object.cpp
1
2
3
4
5
#include "object.h"

using namespace std;

object::object() {}

iblock.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef IBLOCK_H_
#define IBLOCK_H_

#include <iostream>

#include "object.h"

class iblock : public object
{
	public:
		iblock ();
		char type;
		char getType();
};

#endif 

iblock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "iblock.h"

using namespace std;

iblock::iblock()
{
	type = '@';
}

char iblock::getType()
{
	return type;
}

empty.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef EMPTY_H_
#define EMPTY_H_

#include <iostream>

#include "object.h"

class empty : public object
{
	public:
		empty ();
		char type;
		char getType();
};

#endif 

empty.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "empty.h"

using namespace std;

empty::empty()
{
	type = ' ';
}

char empty::getType()
{
	return type;
}


anyone know what's wrong?
Last edited on
Hi
Inside your initializeBoard function you've got:
1
2
iblock x;
empty j;

These are two local variables, created on the stack, that will expire as soon as you reach the end of the function. Yet you're assigning their addresses to your board array.
A very quick (and very horrible) fix would be to declare these as static:
1
2
static iblock x;
static empty j;

But you should really revisit this whole design if you want to take the code any further, I think.
Your 'board' should be a separate class on its own, and I'd try to move away from arrays and pointers altogether and use std::vector instead - you'll never regret it ;)
Regards, keineahnung



keineahnung hat Ahnung. ;)
Wollte das selbe schreiben.
Thanks for the reply keineahnung. I figured my design had some flaw in it because I couldn't really see a way around it.

Just out of curiosity, if the variables expire as soon as the function is over, then how come my program was able to print out at least the first line of "@"? And what exactly does static do (and why would it be a horrible idea?)?
Hi
@jkayme
Just out of curiosity, if the variables expire as soon as the function is over, then how come my program was able to print out at least the first line of "@"?

Actually on my system it didn't - it just segfaulted straight away. So I think you could say the results will be undefined, which is never a good sign.

And what exactly does static do (and why would it be a horrible idea?)?

Erm, I don't have a textbook definition to hand (- look to your favourite C++ reference for that ;). But in this example it basically means the variables 'live on' after the function ends, so the addresses you assigned remain valid for the lifetime of the program.
A bad idea because your data should be packaged up inside the private implementation of the relevant class, with access controlled by a public interface. That's the C++ way :)
@Caligulaminus
keineahnung hat Ahnung. ;)
Wollte das selbe schreiben.


keineahnung tut manchmal so, als ob er Ahnung habe!
Sorry I couldn't get back to you keineahnung, but thanks for your help!
I really appreciate it!
Topic archived. No new replies allowed.