Thread: Two dimensional dynamically allocated array problem

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    11

    Two dimensional dynamically allocated array problem

    Hi everyone.

    I'm having some trouble with dynamically allocating a 2D array. Here's the portion of code dealing with it.

    Code:
    myArrayOne = new int*[size];
    
    for(int i = 0; i < size; ++i)
    {
    	myArrayOne[i] = new int[3];
    }
    	
    for(int i=0;i<size;i++)
    {
    	for(int j=0;j<3;j++)
    	{
    		myArrayOne[i][j] = 0;  // where program crashes
    	}
    }
    The program crashes and (I'm using VS2005) the error tells me that that expression cannot be evaluated. Now obviously I've made a mistake somewhere there but I can't spot it and this way seems to be working for other people judging from a google.

    Any help appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Ok problem solved. I had a different variable in for 'size' in the first for loop. Only saw it when posting up here.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Ehh just so you know there is an easier way that involves some elementary pointer arithmetic.

    Code:
    int main()
    {
       int **myArray;
       int rows = 10, cols = 3;
       myArray = new int*[rows];
       myArray[0] = new int[rows * cols];
       for( int k = 1; k < rows; k++ ) {
          myArray[k] = myArray[0] + (k * cols);
       }
    
       // use myArray
    
       delete [] myArray[0];
       delete [] myArray;
    }
    Basically the key part to this is the for loop. If you can correctly assign the pointers in the first dimension to their respective columns, like here, then you can use the familiar myArray[i][j] syntax with just two allocations to manage.

    You will have to repeat this code when you reallocate for a bigger matrix.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Interesting, it's been a long time since I've messed around with dynamic arrays and the likes. Cheers for the input.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There's an even easier way. Use vector. Unless you're learning, there isn't much reason to use a dynamic array in C++.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    here's another way
    Code:
    int main() {
        int (*a)[10] = new int[3][10];
        a[1][5] = 10; 
        delete [] a;
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  2. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  3. passing a 2dim dynamically allocated array to a funct
    By agerealm in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2004, 06:55 PM
  4. delete dynamically allocated char array
    By xddxogm3 in forum C++ Programming
    Replies: 7
    Last Post: 11-23-2003, 04:57 PM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM