Multi-dimensional arrays using variables

This is a discussion on Multi-dimensional arrays using variables within the C++ Programming forums, part of the General Programming Boards category; Hi all I'm wondering if it's possible to define a mutli-dimensional array using variables, such as the following: Code: int ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    43

    Multi-dimensional arrays using variables

    Hi all

    I'm wondering if it's possible to define a mutli-dimensional array using variables, such as the following:

    Code:
    int CustomCheckersBoard[a][b];
    It doesn't seem anything usual but C++ seems to refuse to accept anything except constant integers when it comes to multi-dimensional arrays. It won't even accept a and b if they're constants.

    Any suggestions about how I might get round this? The context, as you may have guessed, is to allow the user to define the size of a custom checkers board.

    Cheers :-)

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    it will accept it if they're static const or #defined.

    otherwise, you have to implement it as a linked list.

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    The compiler has to be told how large this array, memory to set aside, has to be, so any variables you implement must have an initialized variable of a given size.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by Know_Your_Role
    Any suggestions about how I might get round this? The context, as you may have guessed, is to allow the user to define the size of a custom checkers board.
    If by user you mean a programmer using your checkerboard library, then you could go with something like this:
    Code:
    #include <cstddef>
    
    template<std::size_t N, std::size_t M>
    class Checkerboard
    {
    public:
        // ...
    
    private:
        int board[N][M];
    };
    
    // ...
    
    int main()
    {
        Checkerboard<8, 8> customCheckersBoard;
        // ...
    }
    But if by user you mean a user of your program, then you might consider using a std::vector<std::vector<int> > or Boost.MultiArray.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    You would do well to avoid sufoode's advice since sufoode cannot even demonstrate a 2D dynamic array correctly
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    the stl isn't a separate library.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by sufoode
    take laserlight's advice and go download a whole seperate library just to use dynamic arrays
    Indeed you should, if you are interested in the memory savings available over a std::vector of std::vectors. That said, another possibility is to just implement it yourself using a single std::vector<int>... but it also means writing more code when you can just use an existing library.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    @ OP:

    consider the following:

    Code:
      const unsigned int size = 8;
      int s = 4;  
      const unsigned int size2 = s;
      int arr[size]; // ok
      int arr2[size2]; // compile time error
    do you understand the difference?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by sufoode
    how about just declare a giant array like [1024][1024].

    now the user can sepecify the width and height. problem solved.
    That would be a good solution, except for the potential problem of the giant array being too large to fit into the given memory, e.g., on the stack, or possibly being too small for the desired custom checkerboard. Even if you find the right balance, there would likely be space wastage.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    both of those are really terrible solutions imo.

    i don't share the memory-management-phobia of many on here, but really, suggesting overallocation and/or fileio is just a bad idea.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by sufoode
    The array can be stored on disk and you can seek to file positions to retrieve a value.
    I think that that would defeat the purpose of your suggestion to use a giant array: code would have to be written to implement this functionality, upon which one might as well just use my std::vector<int> suggestion, which is likely to be considerably more efficient anyway.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    This all depends on the amount of memory to consume. Vectors, IMO, will be the best solution, up to the point when excessive reallocation of memory is called for against the vector.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by slingerland3g
    Vectors, IMO, will be the best solution, up to the point when excessive reallocation of memory is called for against the vector.
    Making a guess at the problem domain, I suspect that reallocation is not necessary: once the user has decided on the size, the size of a checkerboard should be fixed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by laserlight View Post
    Making a guess at the problem domain, I suspect that reallocation is not necessary: once the user has decided on the size, the size of a checkerboard should be fixed.

    I think we covered how to dynamically address their memory usage for the array, the tougher question, that plagues everyone, is what amount of memory is optimal to use for a particular aspect of code. ( No silver bullet there!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  2. multi dimensional array definition
    By gazsux in forum C Programming
    Replies: 2
    Last Post: 02-19-2003, 06:35 AM
  3. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM
  4. populating two dimensional arrays
    By garycastillo in forum C Programming
    Replies: 2
    Last Post: 04-05-2002, 07:22 PM
  5. Ok Two Dimensional Arrays help!!!!!!!!!!!
    By incognito in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2002, 09:00 PM

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