Thread: Creating a multideminsional array with new

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    92

    Creating a multideminsional array with new

    This works:

    int * temp = new int [10];

    but this won't work:

    int * temp = new int[10][2];

    I don't understand why. When I try to do the latter I get this error:

    "Cannot convert 'int[*][2]' to 'int*' in initialization.

    I would also like to be able to enter in values to the array like this:

    temp[0][1] = 1;

    I'm sure if that's the right syntax yet because I can't get the array to be initialized properly. What am I doing wrong?

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    You might need to use a double pointer. You need to do something like this:

    int **p;
    p = new int*[n];
    for(int i = 0; i < n; i++)
    *(p+i) = new int[m];

    Someone might be able to tell you another way though.

    Then you can use p[x][y] = z;
    Last edited by Link_26; 03-26-2007 at 08:00 PM.

  3. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Do you know any of the dimensions of the array when you define it? For example, if you didn't know the first dimension, but you knew that the second dimension was 2 you could define it as:
    Code:
    int (*array)[2];
    array = new int[10][2];
    If you don't know either dimension I think (don't quote me on this) that you can get away with:
    Code:
    int **array = new int*[10];
    for (int i = 0; i < 10; i++)
    {
    	array[i] = new int[2];
    }
    Don't quote me on that... ...seriously

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Link_26 View Post
    You might need to use a double pointer. You need to do something like this:

    ...

    Someone might be able to tell you another way though.

    Then you can use p[x][y] = z;
    Your code should be:
    Code:
    int **p;
    p = new int*[n];
    for(int i = 0; i < n; i++)
        p[i] = new int[m];
    But you should avoid dynamic arrays in C++.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    I just tried what you suggested.
    Code:
    int **temp;
    temp = new int*[10][2];  //if I omit the '*' it still gives the same error
    I'm getting this error now:
    "cannot convert 'int*[*][2]' to 'int**' in assignment"

    If I make temp a pointer in the second line (ie *temp = ...) then I get the same error as I stated in my first post.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Quote Originally Posted by Brad0407 View Post
    Do you know any of the dimensions of the array when you define it? For example, if you didn't know the first dimension, but you knew that the second dimension was 2 you could define it as:
    Code:
    int (*array)[2];
    array = new int[10][2];
    If you don't know either dimension I think (don't quote me on this) that you can get away with:
    Code:
    int **array = new int*[10];
    for (int i = 0; i < 10; i++)
    {
    	array[i] = new int[2];
    }
    The first dimension is variable and can be any positive integer. The second dimension will always be 2.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    Quote Originally Posted by King Mir View Post
    Your code should be:
    Code:
    int **p;
    p = new int*[n];
    for(int i = 0; i < n; i++)
        p[i] = new int[m];
    They both work. p[i] = *(p+i)

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Link_26 View Post
    They both work. p[i] = *(p+i)
    That's not all I fixed. You had p as a char **.

    Subscripting is better, because it is easier to understand in a glance.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Thanks so much for the help guys. I just have one more question.

    I understand why this works:

    Code:
    int **p;
    p = new int*[n];
    for(int i = 0; i < n; i++)
        p[i] = new int[m];
    Well kind of, I'm still a little confused on the double pointer but I understand how the 2 dimensional array comes out of it.

    What I don't understand though is how this works:

    Code:
    int (*array)[2];
    array = new int[10][2];
    Could you explain that part to me please?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are you trying to use this in a program or just trying to learn about it? If you are using it in a program a vector of vectors is probably a much better solution.

    If you are trying to learn about it, you should also make sure you know how to delete the arrays as well.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If you know in advance the number of rows, and how long each of the rows is, and you want all the allocated memory to be contiguous to reduce memory fragmentation and cache misses, you can allocate a single 1D array of the necessary length. If all of the rows are the same length, then do something like this (assuming m columns and n rows):
    Code:
      int *p = new int[m*n];
    Then you can access the (i, j) element with the notation p[m*i + j]. To clean up,
    Code:
      delete[] p;
    If the n rows have different lengths, then do
    Code:
      int **p = new int*[n];
      p[0] = new int[num_elements];
    where num_elements is the total number of elements in the array (equal to the sum of the lengths of all the rows). If the rows have length len0, len1, len2, ... then set
    Code:
      p[1] = p[0] + len0;
      p[2] = p[1] + len1;
      // etc.
    Then you can access the (i, j) element with the notation p[i][j]. To clean up,
    Code:
      delete[] p[0];
      delete[] p;
    Edit: If you don't know in advance either the number of rows, or the lengths of each of the rows, then you have to use vectors instead, to be able to do the dynamic resizing (in which case the memory isn't contiguous anymore).
    Last edited by robatino; 03-27-2007 at 12:04 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  3. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM