Two dimensional dynamically allocated array problem

This is a discussion on Two dimensional dynamically allocated array problem within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. I'm having some trouble with dynamically allocating a 2D array. Here's the portion of code dealing with it. ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    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,339
    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, 05:55 PM
  4. delete dynamically allocated char array
    By xddxogm3 in forum C++ Programming
    Replies: 7
    Last Post: 11-23-2003, 03:57 PM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21