Thread: multidmensional dynamic array

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    multidmensional dynamic array

    Hello

    I need a 2-dimensional dynamic bool array with 2 fixed columns and dynamic number of rows.

    Code:
    bool *name[2];
    name = new bool..?
    What would be the proper way of implementing it?

    Thanks a lot for help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Apart from all the weird specialization stuff, what's wrong with vector< vector<bool> >?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Or vector<bool> name_col1, vector<bool> name_col2. Or vector<bool> name[2]?

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Because I'm not allowed to use vector.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you ever have to resize? Or can you just say new bool[some_number][2] and be done with it?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you use std::pair? Then you could use a normal dynamic array of pairs.

    I didn't realize at first you wanted fixed size columns instead of rows. I don't know the proper syntax off the top of my head. My guess is that it involves strategically placed parentheses.

    Edit: tabstop's idea is probably best.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Isn't it just this?
    Code:
    name[0] = new bool[ m ];
    name[1] = new bool[ n ];
    (m and n are arbitrary)

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What is wrong with using bool *name = new bool[ width * height]?

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    630
    I dont have to resize it once its allocated.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Oh ok. Well how about this then:

    Example:
    Code:
    bool *name = new bool[width * height];
    I dunno if anyone had suggested that before, but it sounds good to me.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    630
    This will give me an error:

    Code:
    bool **arr;
    arr= new bool[x][2];

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by master5001 View Post
    Oh ok. Well how about this then:

    Example:
    Code:
    bool *name = new bool[width * height];
    I dunno if anyone had suggested that before, but it sounds good to me.
    Should that really work?

  13. #13
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Code:
    bool** name = new bool*[2];
    and then dynamically allocate the rows (name[][0] and name[][1]).
    Last edited by abachler; 11-04-2008 at 04:58 PM.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yep Behold!

    Example:
    Code:
    #include <iostream>
    
    #define WIDTH 5
    #define WIDTH 5
    
    inline
    bool &getValue(bool *name, size_t x, size_t y)
    {
      return name[y*WIDTH + x];
    }
    
    int main(void)
    {
      bool *name = new bool[WIDTH * HEIGHT];
    
      // do stuff..
    
      std::cout << getValue(name, 3, 4) << std::endl;
    
      return 0;
    }

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by l2u View Post
    This will give me an error:

    Code:
    bool **arr;
    arr= new bool[x][2];
    The return of new bool[x][2] is not of type bool **, but of type bool (*)[2]; that is, a pointer to a bool[2].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic array?
    By BSmith4740 in forum C Programming
    Replies: 8
    Last Post: 06-30-2008, 11:52 AM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM