strange segfault in class

When i run this code,it gives me segmentation fault.
gdb backtrace result:
Program received signal SIGSEGV, Segmentation fault.
0x08048821 in block::setcolor (this=0x0, c=6) at oopcrack-attack.cpp:21
21 color = c;
(gdb) bt
#0 0x08048821 in block::setcolor (this=0x0, c=6) at oopcrack-attack.cpp:21
#1 0x080489ec in gameboard::gameboard (this=0xbfffef4c, a=0)
at oopcrack-attack.cpp:52
#2 0x08048a96 in main () at oopcrack-attack.cpp:69
(gdb)

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cstdlib>
#include "SDL/SDL.h"
#include <SDL/SDL_gfxPrimitives.h>
using namespace std;
struct rgb{int r,g,b;};
class block
{
	int color;
	public:
		int getcolor();
		void setcolor(int);
		rgb torgb();
};
int block::getcolor()
{
	return color;
}
void block::setcolor(int c)
{
	color = c;	
}
rgb block::torgb()
{
	rgb c;
	if(color==1){c.r=255;c.g=140;c.b=0;}//orange
	else if(color==2){c.r=255;c.g=255;c.b=0;}//yellow	
	else if(color==3){c.r=34;c.g=139;c.b=34;}//green
	else if(color==4){c.r=0;c.g=0;c.b=205;}//blue
	else if(color==5){c.r=139;c.g=0;c.b=139;} //dark magneta
	else if(color==6){c.r=128;c.g=128;c.b=128;}//gray
	return c; 	
}
class gameboard
{
	block*** board;
	public:
		gameboard(int);
		void repr();		
};
gameboard::gameboard(int a)
{
	block*** board = new block** [13];
	for(int i=0; i<13;i++)
		board[i]=new block* [6];
	srand(time(0));
	for(int y=11;y>4;y--)
	{
		for(int x=0;x<6;x++)
		{	
			int r=rand() % 6 + 1; 
			board[x][y]->setcolor(r);			 	
		}
	}	
}
void gameboard::repr()
{
	for(int y=11;y>4;y--)
	{
		for(int x=0;x<6;x++)
		{	
			cout<<board[x][y]->getcolor();			 	
		}
		cout<<endl;
	}	
}
int main()
{
	gameboard test(0);
	//test.repr();
}
#0 0x08048821 in block::setcolor (this=0x0, c=6) at oopcrack-attack.cpp:21


Note that this is 0, which is the cause of your segmentation fault. Now the question is, why did the block have an address of 0?

At lines 43-45 you appear to be allocating 13 rows of 6 blocks each.
However at lines 52 and 62, you've reversed the indexes and are referring to board[x][y] where the row varies from 0-5 and the column varies from 11 down to 4. Not sure if this is what you intended, but it seems you're indexing outsode the rows you allocated.
Last edited on
> At lines 43-45 you appear to be allocating 13 rows of 6 blocks each.
except that no block is created.
you've got a 13x6 grid of pointers to block, that were no initialized.
thanks for your helps guys.
i changed code to this and my problem solved:
(added a block constructor too)
1
2
3
4
5
6
7
8
9
10
11
12
13
gameboard::gameboard(int a)
{
	srand(time(0));
	block*** board = new block** [6];
	for(int i=0; i<13;++i)
		board[i]=new block* [13];
	for (int i = 0; i < 6; ++i) 
		for (int j = 0; j < 13; ++j) 
		{
			int r=rand() % 6 + 1;
			board[i][j] = new block(r);
		}
}
Topic archived. No new replies allowed.