Results 1 to 10 of 10

Thread: two dimensional array, size determined dynamically

  1. #1
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default two dimensional array, size determined dynamically

    Hi
    I want to define a two dimensional array whose size N,M is not known at compile time.
    I know one way: define an array of pointers to an array of pointers and then write a little loop that does the allocation.
    However this looks clumsy to me.
    The following does not work
    int array[][] = new int[N][M];
    Is new int [N][M] acceptable as an expression, if so what would the left hand side look like?

  2. #2
    Join Date
    Oct 2009
    Posts
    70
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: two dimensional array, size determined dynamically

    try:

    int* x(int m, int n)
    {
    return new int[m][n];
    }

  3. #3
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: two dimensional array, size determined dynamically

    Peppy,
    Your x function does not compile.

  4. #4
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: two dimensional array, size determined dynamically

    By the way I found a forum discussion that provides suggestions, which are basically that you cannot get around programming with pointers of pointers and a for loop as in this:

    I believe you have to create an array of pointers to an array of ints to get this to work. Here is an example:

    Qt Code:
    1. int **array_ptr; //two * are needed because it is a pointer to a pointer
    2.  
    3. array_ptr=new int*[firstnumber]; //creates a new array of pointers to int objects
    4. for(int i=0; i<firstnumber; ++i)
    5. array_ptr[i]=new int[secondnumber];
    6.  
    7. //now, you can access the members like you can with normal 2d arrays
    8. //like array_ptr[1][3]
    To copy to clipboard, switch view to plain text mode 

    The nice thing is that you can access the array members as if it were a 2d array

  5. #5
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: two dimensional array, size determined dynamically

    I would like to add that it seems that this code using int's can be generalised to arbitrary data types and that we should be able to make use of templates.

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: two dimensional array, size determined dynamically

    I suggest to incorporate that in a class (template or not)

    And careful how you do the "cleanup" (you need to iterate through the "first dimension" delete[] the pointer to type, and after the loop delete[] name of the first_dimension_pointer)

    Or use std::vector<std::vector<your_type> > or QVector<QVector<your_type> > This is the recommended way to achieve two dimension "array"
    Last edited by Zlatomir; 8th June 2010 at 19:59.

  7. The following user says thank you to Zlatomir for this useful post:

    zoz (10th June 2010)

  8. #7
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: two dimensional array, size determined dynamically

    Another simple solution is to just use a 1D array:

    Qt Code:
    1. int* array = new int[rows * columns];
    To copy to clipboard, switch view to plain text mode 

    and do the 2D indexing yourself, which is very simple.

    This approach guarantees that your allocated storage occupies a single, contiguous chunk of memory, something that may be good, bad or indifferent depending on exactly what you're doing. It is probably slightly faster than performing the multiple, small allocations required by the 2D case.
    Last edited by SixDegrees; 8th June 2010 at 22:45.

  9. #8
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: two dimensional array, size determined dynamically

    Thankyou for the suggestions concerning cleanup , vectors and the use of 1d array.

  10. #9
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: two dimensional array, size determined dynamically

    Here is my attempt at sample code that uses a class and double allocation.
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QtDebug>
    3.  
    4. template <typename T> class DynArray2D
    5. {
    6. public:
    7. DynArray2D(int n, int m)
    8. {
    9. _n = n;
    10. _array = new T*[n];
    11. for(int i = 0; i < n; i++)
    12. _array[i]= new T[m];
    13.  
    14. }
    15. void setValue(int n, int m, T v){_array[n][m]=v;}
    16. T getValue(int n, int m){return _array[n][m];}
    17. ~DynArray2D(){
    18. for(int i=0; i < _n;i++)
    19. delete [] _array[i];
    20. delete [] _array;
    21. }
    22.  
    23. private:
    24. T **_array;
    25. int _n;
    26.  
    27. };
    28.  
    29. int main(int argc, char *argv[])
    30. {
    31. QCoreApplication a(argc, argv);
    32. DynArray2D<int> dynIntArray(3,4);
    33. dynIntArray.setValue(1,2,666);
    34. qDebug()<< dynIntArray.getValue(1,2);
    35. return a.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to feraudyh for this useful post:

    zoz (10th June 2010)

  12. #10
    Join Date
    May 2010
    Posts
    1
    Thanks
    40
    Thanked 1 Time in 1 Post

    Default Re: two dimensional array, size determined dynamically

    Thanks for sharing the code
    But i have some little comment, you don't store the second "dimension" value so if you have to iterate the array, you need to remember the size

    Anyway, i agree with what zlatomir said... you can use std::vector or QVector, these are allready tested, they don't leak, have great performance and integrate well with algorithms (stl or qt)

  13. The following user says thank you to zoz for this useful post:

    Zlatomir (12th June 2010)

Similar Threads

  1. Multi-dimensional objects array
    By matulik in forum Newbie
    Replies: 3
    Last Post: 23rd October 2011, 03:20
  2. use QVector as 2 dimensional Array
    By umulingu in forum Qt Programming
    Replies: 3
    Last Post: 1st January 2010, 13:31
  3. How to increase size of QGraphicsView Dynamically
    By Kingofhearts_sri in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2009, 09:54
  4. Replies: 0
    Last Post: 25th June 2008, 19:36
  5. Dynamically changing QGroupBox size
    By T4ng10r in forum Qt Programming
    Replies: 5
    Last Post: 30th March 2007, 16:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.