2D arrays with variable indexes

This is a discussion on 2D arrays with variable indexes within the C++ Programming forums, part of the General Programming Boards category; I'm writing a program (a random map generator, to be exact) that requires a 2D array to created base on ...

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    2D arrays with variable indexes

    I'm writing a program (a random map generator, to be exact) that requires a 2D array to created base on two variables (for the map), height and width. These variables are to be decided at runtime, but the strange way of creating a pointer to a 2D array on the heap has me stumped since the lesser index has to be known. How would I do this with both indexes to be decided at runtime?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You will have to either use something along the lines of nested std::vectors (recommended), or dynamically allocate the memory. Here is a small example:
    Code:
    int x = ...;
    int y = ...;
    
    int** array = new int*[x];
    
    for(int i = 0; i != x; ++i)
       array[i] = new int[y];
    
    // And now, clean up...
    
    for(int j = 0; j != x; ++j)
       delete[] array[i];
    delete[] array;
    If you are not too familiar with pointers, this probably isn't a good route. The vectors are really a better (easier and safer) idea.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    I'm familiar with pointers, but the line of my knowledge is drawn before all of that double indirection and 2D array stuff.

    After I declare the array in the way you posted, I can use the array as normal (array[i][j])?

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yes... You'd use it normally.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    I used this, and kept getting one of those Microsoft error things (the ones where you have the option to send an error report) whenever I ran the program. Any idea what's up?

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    82
    one of those Microsoft error things
    The most common cause of this is trying to access memory that doesn't belong to you. For example, reading 1 past the end of an array.

    I'd also point out that 2d arrays aren't pretty. You'll need to write functions to create and destroy the array, and the syntax for passing 2d arrays to functions is clunky.

    There are two ways to deal with this. If you just need something that works, the vector of vectors is by far the simplest solution. If you want to get a feel for 2d arrays, writing a class (matrix or grid or some such) is a good exercise, as it really begs for encapsulation.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    AH_Tze's points are really good, and I recommend paying attention to them.

    However, to find the error specifically, we will need code.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    While I was writing this post, I thought of the problem. I was trying to access the array in a function I never passed it to (at least I think that's what the problem is). The code is 2500 lines long anyway, so I don't think anyone wants to look at it. If that's not the problem, I'll post the code for you guys to take a look at.

    -thanks

  9. #9
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    I got it to work, but I want to be able to understand it. Tell me if I'm wrong in any of these.

    Code:
    int** array = new int*[x];
    This declares a pointer to a pointer and then initializes it to point to the first element of an array of pointers (the array having x elements).

    Code:
    for(int i = 0; i != x; ++i)
       array[i] = new int[y];
    This assigns the pointer that array[i] points to to an array with i elements.

    Code:
    (*(*(array+i)+j)) = 5;
    This dereferences array (along with adding an offset before the dereference), to access the pointer pointed to by (array+i). Each element of array holds a pointer that holds the address of an int array on the heap so further applying the dereference operator on and adding an offset to array (array[i][j]) dereferences the pointer pointed to by array[i] and accesses the element array[i][j].

    I have a hunch this is right. I didn't do any research, this is just an educated guess. I'm fairly good with pointers already, just not pointers to pointers.
    Last edited by homeyg; 01-22-2005 at 04:29 PM.

  10. #10
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Zach L.
    Code:
    int x = ...;
    int y = ...;
    
    int** array = new int*[x];
    What's wrong with:
    Code:
    int array[x][y];
    ?

  11. #11
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    You can't have a 2D array with both indexes unknown.

    Edit: I guess you can. Nevermind.
    Last edited by homeyg; 01-22-2005 at 07:13 PM.

  12. #12
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    Quote Originally Posted by Kleid-0
    What's wrong with:
    Code:
    int array[x][y];
    ?
    Array sizes have to be constant expressions. Because x and y aren't const, the declaration is ill-formed. If your compiler supports it then it does so as a nonstandard extension.

  13. #13
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Is it standard for 1D arrays to not have constant sizes?

  14. #14
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>Is it standard for 1D arrays to not have constant sizes?
    No, that's a consistent rule. This is legal:
    Code:
    int main()
    {
      int a[5];
      int b[10][10];
    }
    And this is legal:
    Code:
    int main()
    {
      const int size1 = 5;
      const int size2 = 10;
      int a[size1];
      int b[size2][size2];
    }
    But this is not:
    Code:
    int main()
    {
      int size1 = 5;
      int size2 = 10;
      int a[size1];
      int b[size2][size2];
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. 1D and 2D Arrays
    By Rajin in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 06:23 PM
  3. 2d arrays: H e l p!
    By scuba22 in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2002, 04:54 PM
  4. pointers to 2d arrays
    By Diamonds in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2002, 05:58 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

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