Thread: help with multidimensional array

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    81

    help with multidimensional array

    if 4 lems are created in a 2 by 2 array, why does the for statement beneath the while statement output index 2 and 3 as being NULL?

    Could it be the fact that I flattened the array by accessing it sequentially without the row/column notation? I thought that a multidimensional array was stored in memory sequentially anyways, so the way I am doing it should be fine....

    Code:
    	for (int r=0; r <= rows; r++)
    	{
    		for (int	c=0; c <= cols; c++)
    		{
    			lems[r][c] = new Lem(r, c, (r*c)+c);   // this statement is executed 4 times
    			cin >> lems[r][c]->agenda;
    			cout << lems[r][c]->agenda << endl;			   			
    		}
    	}
    
    	Lem** plems = (Lem**)lems; 
    
    	for(int i = 0; i < size; i++)
            {
    		if ( plems[i] == 0 ) 
    			cout << "huh?" << i << endl;   // 2 and 3 are NULL?   
            }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you show the declaration of lems, rows and cols?

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I'm afraid you've left out important information: what are size, rows and cols, and how is lems declared.

    Code:
    for (int r=0; r <= rows; r++)
    for (int	c=0; c <= cols; c++)
    These lines look like almost sure errors (loops for one more row and column than rows and cols).

    Code:
    Lem** plems = (Lem**)lems;
    Also looks a bit suspicious.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    You've probably heard that an array is actually a pointer to the first element of your array.
    A multi-dimensional array, is actually an array of arrays (or an array of pointers).

    What happens is that your lems array stores pointers sequentially, but you have no guarantee that these pointers will be placed sequentially inside the memory.

    What this means is that lems[n] actually returns an array of Lems wich you can access using the [] operator.

    The memory should look something like this.

    lems: { address1 , address2 }
    ... anything ...
    address1: { Lem11 , Lem12 }
    ... more anything ...
    address2: { Lem21 , Lem22 }

    So what you get if you ask for lems[2] (or anything above) is what comes after address2 in the memory (what I called "anything") which happens to be just NULL in your case.


    Also, and I got it wrong above, lems is actually an array of arrays to pointers to Lems (since you instantiate them with new). So if you consider that an array is actually a pointer, it means that lems is a Lem***, and your cast is wrong.
    Anyway, as discussed, you couldn't use plems like that since the data is not sequentially placed in the memory.
    Last edited by krappa; 10-30-2008 at 10:25 AM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Since a[i][j][k] is just syntactic sugar for *(*(*(a + i) + j) + k)
    it would seem that multidimensional arrays are stored contiguously.
    Here's a program analogous to yours but using int instead of Lem.
    It seems to work just fine.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        const int rows = 2, cols = 2;
        int* a[rows][cols];
    
        for (int r = 0, i = 1; r < rows; ++r)
            for (int c = 0; c < cols; ++c)
                a[r][c] = new int(i++);
    
        int** p = (int**) a;
        for (int i = 0; i < rows * cols; ++i)
        {
            cout << i;
            if ( p[i] ) cout << " = " << *p[i] << endl;
            else        cout << " zero pointer\n";
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Help -- allocating memory in a multidimensional array
    By jonathan.plumb in forum C Programming
    Replies: 10
    Last Post: 05-20-2009, 11:04 PM
  3. Pointer to multidimensional array
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 11:17 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM