C 2-D arrays

Estukaamiaftadai's Avatar, Join Date: Jun 2007
Newbie Member
Hi, I'm trying to initialize a two dimensional C array in C++, but I must be doing something wrong because I'm getting some screwy results. Here's my code:

Code:
	board = (char**)malloc(boardWidth);
	for(int x = 0; x < boardWidth; x++)
	{
		board[x] = (char*)malloc(boardLength);
		for(int y = 0; y < boardLength; y++)
		{
			board[x][y] = EMPTY;
			cerr << int(board[x][y]) << " ";
		}
		cerr << endl;
	}
	cerr << endl;

	for(int x = 0; x < boardWidth; x++)
	{
		for(int y = 0; y < boardLength; y++)
		{
			cerr << int(board[x][y]) << " ";
		}
		cerr << endl;
	}
Note that the first big for loop allocates the double array and prints out its contents after setting each position to EMPTY (here, EMPTY is defined as 0) and the second big for loop prints out the contents again. Here's the output I'm getting:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

112 36 63 0 -112 36 63 0 -80 36 63 0 -48 36 63 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

As you can see, somehow there's data being written in between the two loops, even though one is directly after the other. So I figure I must be doing something wrong in allocating memory or referencing it.

Note that 'board' is declared as 'char** board;' and is a global variable; however, it isn't touched until this code. BoardWidth and BoardLength are integers with sensible values (10 or 20 or so).

Can anyone identify the problem here? Thank you.

Last edited by shabbir; 4Jun2007 at 08:05.. Reason: Code block - http://www.go4expert.com/forums/misc.php?do=bbcode#code
0
Estukaamiaftadai's Avatar, Join Date: Jun 2007
Newbie Member
Also: I'm not using the sizeof operator in malloc because the size of a char is 1 (though I trued putting it in there and nothing changed), and I tried replacing the EMPTY with 0 and '0', but the problem persisted.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
The writing is being done by cerr.

I would suggest that your array, by normal standards, is "on its side". Rows (height) are generally considered to be the least rapidly varying index.

I would also suggest that it's not a good thing to mix C and C++. C is nearly, but not completely, a subset of C++. The result is almost always a bad (or at least, less than optimal) program.

Most C++ compilers will compile true C, given the appropriate file extension or settings. I would just suggest that you write one or the other.
0
Estukaamiaftadai's Avatar, Join Date: Jun 2007
Newbie Member
Ah! Never mind, I just figured it out. The problem is in the first malloc statement. Recall:

Code:
board = (char**)malloc(boardWidth);
I allocated memory for a char array, but I should be allocating memory for a pointer array. Hence, the size should be boardWidth * sizeof(char*), not just boardWidth * sizeof(char).

Really silly mistake now that I think of it. I should have caught it sooner.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
You have to cast malloc's return in C++, but you shouldn't be using malloc with C++. In C, you don't have to (and don't want to) cast the return. A good template for malloc which adapts if you change a pointer type, is myPtr = malloc (sizeNeeded * sizeof (*myPtr));