C 2-D arrays

Discussion in 'C' started by Estukaamiaftadai, Jun 3, 2007.

  1. Estukaamiaftadai

    Estukaamiaftadai New Member

    Joined:
    Jun 3, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Jun 4, 2007
  2. Estukaamiaftadai

    Estukaamiaftadai New Member

    Joined:
    Jun 3, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  4. Estukaamiaftadai

    Estukaamiaftadai New Member

    Joined:
    Jun 3, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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));
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice